Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I write unmanaged C# code?

Status
Not open for further replies.

pmccombs

Programmer
May 29, 2003
7
0
0
US
First of all, I need a little clarification:

Just what is the difference between managed and unmanaged code? I have a general idea that unmanaged code is code found in DLLs, such as the system APIs.

I'd like to write code like this. I have a need to write an external library that is available to a Power Builder application.

Can I write it in C#?
 
Yes, you can write unmanaged code in C# (it's called "unsafe" code). But it's only really useful for getting a speed boost -- you can't write a native Win32 DLL using C#. You can, however, write a COM object that may be able to be called from PowerBuilder (don't know anything about PowerBuilder, sorry).

Chip H.
 
I'd say give it a shot...write a small dll and try it. You may find that you have to sign the assembly...look at thread184-559623 where I explained how to do it for somebody that needed to use .NET DLL in VFP, don't worry only the last part of my posted suggestion is VFP code and the rest shows the basic process for C# and at the end instead of trying it out in VFP just go over and try out your new math dll in Power Builder.

Slighthaze = NULL
 
Regarding unmanaged C# code -

Thanks for the replies! Here is what my own research has turned up on the subject:

In Microsoft's Inside C# 2nd edition, it states, "unsafe code does not imply unmanaged code" or something along those lines. So, although I believe it is possible to actually write unmanaged code using the unsafe keyword, I don't see that Microsoft has made it easy to do so. It is still unclear in my mind whether or not unsafe code is compiled to native machine code, or if it is compiled into CLR readable code.

In particular, I wonder how I would go about exporting unsafe C# functions so that they can be used in other unmanaged code. The Dev studio provides a nice template ("MFC DLL") that does just this sort of thing on the C++ side, but there is no equivalent C# project template. In C++, I can mark my functions as extern "C" and use __stdcall, which does some specific things with the stack that I need in order to call APIs in a DLL from Power Builder and other unamanaged code via dynamic linkage.

in addition, I have to insert the decorated function names in my DLL's .DEF file, or else use __declspec(dllexport) keyword on the C++ side. I do not see how to do this easily in C#.

The Microsoft book does discuss calling C# code from unmanaged code, and the method to do this is via COM. However, this is not the way Power Builder wants to communicate with external functions. Rather, it uses traditional dynamic linkage.

So - what I have done is write unmanaged C++ code using the Dev studio MFC DLL template, and this DLL works fine with Power Builder. I don't think that C# is intended for unmanaged code, at least I don't see that this is what people are doing with it.
 
pmccombs -

That's exactly what I meant - you can't write traditional Win32 DLLs in C# because there's no way to specify an entry point (either via the old-school .DEF file or the newer __declspec(dllexport)). So as you've found out, it sounds like C++ is needed. Sorry.

C# does have a lot of nice stuff in it (like hardly ever having to worry about memory management), but you've discovered one of it's limitations.

Chip H.
 
I know this thread is a year old but I'm having this problem with some old code. So, there isn't any method to set an entry point on a C# dll? My option is to rewrite it in C++??? Just checking before I start down that path.

I rewrote an existing C++ dll because we didn't have the source code. I wrote a new C# dll (because I know C#) and want to call it from an old FoxPro app. I'm getting an error about not being able to find the entry point.

Thanks.
 
Right - a C# dll is not a Win32 dll. It doesn't have an entry point that can be used when calling it in the manner that you would for calling a Win32 api.

To call it from another language, program, etc., you'd have to use .NET (duh), or COM (tell the IDE to write a COM-callable wrapper).

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top