Exporting functions from C# .dll for other programs
-
Hi I need to write a C# .dll library which exports functions accessible from good old win32 API applications. (not the usual other way around, calling win API functions from C#) Is this possible? If so, how do I make them as regular (global) functions, without being nested in some namespace and class? The win32 api application requires for example "void FunctionA()" and "void FunctionBee()" to be exported in my dll, but as far as I know, C# does not allow for functions to be on their own, without being in a namespace and class. Other than that, I do not know how to mark them as "exported", some attribute or something? Please point me in the right direction, any help appreciated. Thanks, H.
-
Hi I need to write a C# .dll library which exports functions accessible from good old win32 API applications. (not the usual other way around, calling win API functions from C#) Is this possible? If so, how do I make them as regular (global) functions, without being nested in some namespace and class? The win32 api application requires for example "void FunctionA()" and "void FunctionBee()" to be exported in my dll, but as far as I know, C# does not allow for functions to be on their own, without being in a namespace and class. Other than that, I do not know how to mark them as "exported", some attribute or something? Please point me in the right direction, any help appreciated. Thanks, H.
Member 1033907 wrote:
If so, how do I make them as regular (global) functions, without being nested in some namespace and class?
Not possible in .NET. You can't 'export' them, you'd need to write a COM dll and access them via COM.
Christian Graus Driven to the arms of OSX by Vista. "I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )
-
Hi I need to write a C# .dll library which exports functions accessible from good old win32 API applications. (not the usual other way around, calling win API functions from C#) Is this possible? If so, how do I make them as regular (global) functions, without being nested in some namespace and class? The win32 api application requires for example "void FunctionA()" and "void FunctionBee()" to be exported in my dll, but as far as I know, C# does not allow for functions to be on their own, without being in a namespace and class. Other than that, I do not know how to mark them as "exported", some attribute or something? Please point me in the right direction, any help appreciated. Thanks, H.
Christian is correct. The only "easy" interface you have is to expose your classes, properties, and method is through COM. You could try to host the .NET CLR in your Win32 app, but that's a severe pain in the ass to do. Read[^]
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
Member 1033907 wrote:
If so, how do I make them as regular (global) functions, without being nested in some namespace and class?
Not possible in .NET. You can't 'export' them, you'd need to write a COM dll and access them via COM.
Christian Graus Driven to the arms of OSX by Vista. "I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )
That is bad news, I'll probably need to reconsider the whole approach. One last shot - if I leave out the requirement that they need to be standalone functions and write something like
namespace Space
{
public class Something
{
public static void FunctionA()
{
// do something
}
}
}Can this
FunctionA
be somehow accessed from an unmanaged C++ win32 application? (no fancy com, mfc, nothing). If so, could you give me a little clue how (on the c++ side)? Or another way to make it work with just the C++ app and the C# dll? (I have access to both) Thanks in advance, H. -
Christian is correct. The only "easy" interface you have is to expose your classes, properties, and method is through COM. You could try to host the .NET CLR in your Win32 app, but that's a severe pain in the ass to do. Read[^]
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008Thanks. Please read my answer above, I'll be glad if you have something to say. Otherwise I'll probably forget about C# altogether. BTW: "through COM" means that I 1) write the C# .dll with the functions (as public static methods) 2) write another library which receives calls from the win32 application and forwards them to the C# dll? Thanks, H.
-
That is bad news, I'll probably need to reconsider the whole approach. One last shot - if I leave out the requirement that they need to be standalone functions and write something like
namespace Space
{
public class Something
{
public static void FunctionA()
{
// do something
}
}
}Can this
FunctionA
be somehow accessed from an unmanaged C++ win32 application? (no fancy com, mfc, nothing). If so, could you give me a little clue how (on the c++ side)? Or another way to make it work with just the C++ app and the C# dll? (I have access to both) Thanks in advance, H.Member 1033907 wrote:
Can this FunctionA be somehow accessed from an unmanaged C++ win32 application? (no fancy com, mfc, nothing).
No.
Member 1033907 wrote:
Or another way to make it work with just the C++ app and the C# dll? (I have access to both)
Using C# to generate the .DLL, we've already told you the options you have.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
Member 1033907 wrote:
Can this FunctionA be somehow accessed from an unmanaged C++ win32 application? (no fancy com, mfc, nothing).
No.
Member 1033907 wrote:
Or another way to make it work with just the C++ app and the C# dll? (I have access to both)
Using C# to generate the .DLL, we've already told you the options you have.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008Ok, thanks, no c# then. Out of curiosity - what project type one selects when creating COM dll in Visual Studio? Is it "ATL Project" or something else? I've never done that (and probably won't). H.
-
Ok, thanks, no c# then. Out of curiosity - what project type one selects when creating COM dll in Visual Studio? Is it "ATL Project" or something else? I've never done that (and probably won't). H.
Just a class library. The COM exposure is all in how you attribute your code.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008