FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour How to convert LIB from .NET dll file
Posts: 866
Joined: Tue Oct 16, 2007 08:57 AM
How to convert LIB from .NET dll file
Posted: Sun Jan 19, 2025 06:26 AM

Dear Antonio,

I have a .NET dll file

How to convert to LIB file for Harbour/xHarbour?

Thanks a lot.

Best Regards,



Richard



Harbour 3.2.0dev (r2503251254) => Borland C++ v7.7 32bit

MySQL v8.0

Harbour 3.2.0dev (r2503251254) => Borland C++ v7.7 64bit
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: How to convert LIB from .NET dll file
Posted: Sun Jan 19, 2025 07: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:
// 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);
}
// 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;
}
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 866
Joined: Tue Oct 16, 2007 08:57 AM
Re: How to convert LIB from .NET dll file
Posted: Tue Jan 21, 2025 04:52 AM
Antonio Linares wrote: 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:
// 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);
}
// 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.
Best Regards,



Richard



Harbour 3.2.0dev (r2503251254) => Borland C++ v7.7 32bit

MySQL v8.0

Harbour 3.2.0dev (r2503251254) => Borland C++ v7.7 64bit

Continue the discussion