Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, September 27, 2012 7:25 PM
Hello Gurus !
Perhaps you can help this old man answer a difficult question ....
I wondered if a DLL, C# or C++, could be written to support 'StdCall' convention in such a way that a client writing code using a compiler from outside the Microsoft Windows community could use the DLL. Does anyone here have any experience with that ?
I have significant C++ / C# experience and have created tons o' DLLs, but always for a fellow Windows user, never one intended for a gcc-usin' client. (heck, I didn't even know there were non-Windows users !)
Thanks for any information you can provide.
bill
All replies (6)
Thursday, September 27, 2012 7:40 PM ✅Answered
You can write a DLL in C++ that uses __stdcall. See: http://msdn.microsoft.com/en-us/library/zxk0tw93(v=vs.71).aspx
C#, however, creates its DLLs as .NET assemblies, which really tends to lead to only being usable from outside either by other .NET languages (unless you host the framework) or via COM interop. You can't call methods of a C# type directly like you can with "normal" (native) DLL exported functions.
Reed Copsey, Jr. - http://reedcopsey.com
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
Thursday, September 27, 2012 8:02 PM ✅Answered
C# can, of course, consume unmanaged stdcall functions through pinvoke...
[DllImport("foo.dll",CallingConvention=CallingConvention.StdCall)]
void foo();
...but this is, I think, the opposite of what you asked for.
So you can also call back to managed code from your unmanaged code by offering a callback (see example). And this is the way I would try to approach anything that uses both managed and unmanaged code. I would try to make use of an existing managed host somewhere and turn the unmanaged component into a dll somehow. It's the least amount of work.
Now that may not be an option because you may just want to make use of one tiny little piece of managed code in a much larger unmanaged application, so if the first way is a no-go and you really want to start with unmanaged code, and call into managed code then you are also looking at hosting the CLR. (see complicated example.) It's doable, but read the docs carefully. There are many considerations necessary to host properly.
Thursday, September 27, 2012 8:05 PM
"You can't call methods of a C# type directly like you can with "normal" (native) DLL exported functions"
Native C++ code might be an option to explore, along with C# .NET. As I recall, to work with native C++ in Visual Studio, one selects "Other Languages/Visual C++/Win32" template ... is that correct ? I will very llikely try both methods.
Thanks very much for your guidance on this. 'Preciate it.
bill
Thursday, September 27, 2012 8:58 PM
"You can't call methods of a C# type directly like you can with "normal" (native) DLL exported functions"
Native C++ code might be an option to explore, along with C# .NET. As I recall, to work with native C++ in Visual Studio, one selects "Other Languages/Visual C++/Win32" template ... is that correct ? I will very llikely try both methods.
Thanks very much for your guidance on this. 'Preciate it.
bill
Yes. You can use any of the C++ library project types that don't mention "CLR" if you want to create native code. These can all export functions as normal __stdcall calling convention routines.
Reed Copsey, Jr. - http://reedcopsey.com
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
Thursday, September 27, 2012 9:30 PM
Gentlemen: Reed, Wyck
I Thank You Both for your help .... this is gonna be a fun adventure. You'll probably see many more posts from me in the next two or three weeks 'til I get this hammered out.
Thanks again.
bill
Friday, September 28, 2012 7:50 AM
See if this helps - https://sites.google.com/site/robertgiesecke/Home/uploads/csharpprojecttemplateforunmanagedexports
Sameer
If this answers your question, please Mark it as Answer. If this post is helpful, please vote as helpful.