Dear Richard,
Yes, it's possible to use a .NET DLL from a C application, although it requires specific steps since they are different environments. There are two main approaches:
COM Interop
First, you need to expose the .NET DLL as a COM component
Register the .NET assembly using regasm.exe
Then you can use the functionality from C through the COM interface
C++/CLI as a bridge
Create an intermediate DLL using C++/CLI that serves as a "wrapper"
This DLL can communicate with both native C code and .NET code
The C application calls the wrapper DLL, which in turn calls the .NET DLL
Here's a basic example using the C++/CLI approach:
Code: Select all | Expand
// Wrapper.h - Intermediate DLL in C++/CLI
#pragma once
// Exported function that the C application can call
extern "C" __declspec(dllexport) int CallDotNetFunction(int param);
// Wrapper.cpp
#include "Wrapper.h"
#using "MyNetDLL.dll"
int CallDotNetFunction(int param) {
// Call the class/method from the .NET DLL
MyNetDLL::MyClass^ instance = gcnew MyNetDLL::MyClass();
return instance->MyMethod(param);
}
Code: Select all | Expand
// C Application
#include <windows.h>
typedef int (*CallDotNetFunction)(int);
int main() {
HMODULE hDll = LoadLibrary("Wrapper.dll");
CallDotNetFunction func = (CallDotNetFunction)GetProcAddress(hDll, "CallDotNetFunction");
int result = func(42);
FreeLibrary(hDll);
return 0;
}