c# calling conventions...

Thad T 6 Reputation points
2022-01-25T23:33:38.08+00:00

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

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,159 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,504 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Ken Tucker 5,846 Reputation points
    2022-01-26T00:58:44.363+00:00

    I think you need to use pinvoke to call a c dll.

    https://learn.microsoft.com/en-us/dotnet/standard/native-interop/pinvoke

    0 comments No comments

  2. Bruce (SqlWork.com) 54,621 Reputation points
    2022-01-26T02:27:22.897+00:00

    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.

    0 comments No comments

  3. Minxin Yu 9,781 Reputation points Microsoft Vendor
    2022-01-26T06:35:00.973+00:00

    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.

    0 comments No comments