I think you need to use pinvoke to call a c dll.
https://learn.microsoft.com/en-us/dotnet/standard/native-interop/pinvoke
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
To all MSVC'ers,
With asking this question, I'ved search the internet hi and low for answers on: "what are the C# calling conventions" towards calling "C" routines. I have many simple and straight forth C" routines ,all contained within a WIndows DLL file, that I want to call. I've been shot down down many many times on developer approached web sites using this same approach that I'm doing now, just asking for some help. I've learned c# from/by C# programming examples in its simple forms. This is how I put this program together. Along with a lot of determination. I've been using C#'s stringbuilder to match "C"'s char data types and am not doing too bad. I also trying to match C#'s int to "C"'s int and so forth, and again I'm not doing too bad. Like I'm trying for a 1 for 1 level approach. The C# program and "C" routines(contained within this DLL file) are doing pretty good. Now, I'm working out some "C" program problems. My experiences with MSVC environments( C# and "C") are on the positive side. But there is catch-22 that I've experienced. The program(C#) and DLL "C" works well together while in the MSVC debug environment. BUT when I put both projects into release mode and run the C# program, outside the msvc environment, (which the C# program calls the "C" routines that are contained the dll file), it abends right quick when I go a certain direction = leaving no stack dump and any other piece(s) of helpful information of any kinds. In being from the old college, programming on older medium frame computers(e.g.: DEC Vax/Vms 11780) , when old college programming projects abends, it leaves or provides visual stack dumps and other pieces of info were very very helpful, (I like it and I'm use to). I ask why not now??
Further info, as requested:
I can make the "C" DLL File ( that contains all my "C" routines) real easy.. Likewise with the C# program.
On the DLL side, I export all the routine names; unmangled.
On the C# side, I import them with routine names/ with parameter declarations from the dll/definition file(.def) ( but put them into proper C# forms).
But it all comes down to the C# calling conventions toward "C" know-how. It is no where to be found - I can't find it. Do you have the know-how on how it is done, and could help me out., in forms of examples - showings...
I am using the MSVC 2019 environment.
But, thanks for any possible help...
auroraprjt
I think you need to use pinvoke to call a c dll.
https://learn.microsoft.com/en-us/dotnet/standard/native-interop/pinvoke
Unlike c, c# is strongly typed language, and uses a garbage collector rather than a heap. In C# primitive data types are objects, though they can unboxed to be more like c objects.
C# parameters are as passed object instances (descriptor) via a stack frame. In addition c# is a virtual machine language. The vm language is called MSIL. The calling conventions of MSIL are defined, but not the native machine implementation which is handled by the jit compiler if used.
https://en.wikipedia.org/wiki/Common_Intermediate_Language
c# also has the notion of unsafe pointers along with pinvoke are used when calling native code. See
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/unsafe-code
So, to answer your question, you don’t need to know c# calling convention, but rather the c calling convention, and how to map them to pinvoke.
You can also use c# dllexport to make c# callable from c
https://learn.microsoft.com/en-us/cpp/cpp/dllexport-dllimport?view=msvc-170
Note: windows nt used the same design as vax/vms as it was designed by the same person. Unfortunately to be compatible with windows, it used the windows calling conventions (__stdcall) , rather than the native c calling conventions. (__cdecl)
There was even a vax alpha version of windows nt back when DEC still existed.
Hi, @Thad T
To use dllexport, you can also refer to the article: Call Unmanaged DLLs from C#.
Best regards,
Minxin Yu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.