How to convert LIB from .NET dll file

Post Reply
User avatar
richard-service
Posts: 806
Joined: Tue Oct 16, 2007 8:57 am
Location: New Taipei City, Taiwan
Contact:

How to convert LIB from .NET dll file

Post by richard-service »

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 (r2402101027) => Borland C++ v7.7 32bit
MySQL v8.0 /ADS v10
Harbour 3.2.0dev (r2011030937) => Borland C++ v7.4 64bit
User avatar
Antonio Linares
Site Admin
Posts: 42393
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 9 times
Been thanked: 41 times
Contact:

Re: How to convert LIB from .NET dll file

Post 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;
}
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
richard-service
Posts: 806
Joined: Tue Oct 16, 2007 8:57 am
Location: New Taipei City, Taiwan
Contact:

Re: How to convert LIB from .NET dll file

Post 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.
Best Regards,

Richard

Harbour 3.2.0dev (r2402101027) => Borland C++ v7.7 32bit
MySQL v8.0 /ADS v10
Harbour 3.2.0dev (r2011030937) => Borland C++ v7.4 64bit
Post Reply