Page 1 of 1

How to convert LIB from .NET dll file

Posted: Sun Jan 19, 2025 6:26 am
by richard-service
Dear Antonio,

I have a .NET dll file
How to convert to LIB file for Harbour/xHarbour?

Thanks a lot.

Re: How to convert LIB from .NET dll file

Posted: Sun Jan 19, 2025 7:12 pm
by Antonio Linares
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;
}

Re: How to convert LIB from .NET dll file

Posted: Tue Jan 21, 2025 4:52 am
by richard-service
Antonio Linares wrote: Sun Jan 19, 2025 7:12 pm 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;
}
Dear Antonio,

I'll try it again.
Thank you.