472,792 Members | 2,231 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,792 software developers and data experts.

Call C/C++ CALLBACK DLL from C# (PInvoke? Wrapper class?)

Hi,

I have an old C++ GUI Application CPPAPP.exe that calls a C DLL
library RULE.DLL through a C++ class wrapper LoadRule.CPP.

Now I need to call the C DLL RULE.DLL from C# GUI application
CSAPP.exe. I need help to convert LoadRule.CPP to a C++

BridgeDLL, and I need help on how to convert the call to the BridgeDLL
from C#.
//
************************************************** ***********************************
//Rule.h: header file in C DLL RULE.DLL

#ifndef __RULE__
#define __RULE__
#ifdef __cplusplus
extern "C" {
#endif

typedef DWORD HRULE; /* Handle used in all calls */

typedef BOOL (CALLBACK* RULECALLBACK)(UINT nEvent, /* Event */
LPARAM lPar1, /*
Parameter 1 */
LPARAM lPar2, /*
Parameter 2 */
LPARAM dwExtra); /* Extra
data */
/******************************/
/* Diagnostics data structure */
/******************************/

typedef struct tagDIAGNOSTICS {
DWORD dwUsage; /* No. of times has beed used */
DWORD dwReserved0; /* For future use */
} DIAGNOSTICS;
typedef DIAGNOSTICS FAR * LPDIAGNOSTICS;

/***********************/
/* Function prototypes */
/***********************/

/************************************************** ***************/
/* Start new I/O session. */
/* Entry: hWnd = window handle of caller, could be NULL */
/* lpFn = address of callback handler */
/* lPar = extra info passed to callback function */
/* lphRule = where to copy session handle if open */
/* call was Successful. */
/* Exit: RULEERR_OK : connected OK */
/* RULEERR_MEMERR : memory allocation error */
/* RULEERR_UNKNOWN : unknown error */
/* RULEERR_NOCALLBACKFNC : No callback function given */
/************************************************** ***************/

int WINAPI Rule_NewSession(HWND hWnd,
RULECALLBACK lpfnCallback,
LPARAM lPar,
HRULE FAR * lphRule);
int WINAPI Rule_EndSession(HRULE hRule);
int WINAPI Rule_ReadDiagnostics(HRULE hRule,LPDIAGNOSTICS lpDiag);
int WINAPI Rule_WriteDiagnostics(HRULE hRule,LPDIAGNOSTICS lpDiag);
#ifdef __cplusplus
}
#endif

#endif // __RULE__

//
************************************************** ***********************************
//LoadRule.h: C++ wrapper header file for DLL RULE.DLL

#ifdef __cplusplus
extern "C" {
#endif

/* Definitions for LoadRule.C module */

HINSTANCE LoadRule(LPTSTR lpszPath);
void UnloadRule(HINSTANCE hInst);

#ifdef __cplusplus
}
#endif
//
************************************************** ***********************************
// LoadRule.cpp : C++ wrapper implementation file

#include "LoadRule.h"

typedef int (WINAPI *RULE_NewSession)(HWND hWnd,
RULECALLBACK lpfnCallback,
LPARAM lPar,
HRULE FAR * lphRule);

typedef int (WINAPI *RULE_EndSession)(HRULE hRule);
typedef int (WINAPI *RULE_ReadDiagnostics)(HRULE hRule,LPDIAGNOSTICS
lpDiag);
typedef int (WINAPI *RULE_WriteDiagnostics)(HRULE hRule,LPDIAGNOSTICS
lpDiag);
static RULE_NewSession lpfnRule_NewSession;
static RULE_EndSession lpfnRule_EndSession;
static RULE_ReadDiagnostics lpfnRule_ReadDiagnostics;
static RULE_WriteDiagnostics lpfnRule_WriteDiagnostics;

/* Local functions */

static BOOL LoadFunctionPointers(HINSTANCE hInst);
static void ClearFunctionPointers(void);
HINSTANCE LoadRule(LPTSTR lpszPath)
{
HINSTANCE hInst;

hInst=LoadLibrary(lpszPath);
if (!hInst) return hInst;

if (!LoadFunctionPointers(hInst)) {
FreeLibrary(hInst);
hInst=NULL;
}
return hInst;
}
void UnloadRule(HINSTANCE hInst)
{
FreeLibrary(hInst);
ClearFunctionPointers();
}

/************************************* Local functions
********************************/

static BOOL LoadFunctionPointers(HINSTANCE hInst)
{
lpfnRule_NewSession = (RULE_NewSession)
GetProcAddress(hInst,TEXT("Rule_NewSession"));
lpfnRule_EndSession = (RULE_EndSession)
GetProcAddress(hInst,TEXT("Rule_EndSession"));

lpfnRule_ReadDiagnostics = (RULE_ReadDiagnostics)
GetProcAddress(hInst,TEXT("Rule_ReadDiagnostics")) ;
lpfnRule_WriteDiagnostics = (RULE_WriteDiagnostics)
GetProcAddress(hInst,TEXT("Rule_WriteDiagnostics") );

if (!lpfnRule_NewSession ||
!lpfnRule_EndSession ||
!lpfnRule_ReadDiagnostics ||
!lpfnRule_WriteDiagnostics ){

ClearFunctionPointers();
return FALSE;
}
return TRUE;
}

static void ClearFunctionPointers(void)
{
lpfnRule_NewSession=NULL;
lpfnRule_EndSession=NULL;
lpfnRule_ReadDiagnostics=NULL;
lpfnRule_WriteDiagnostics=NULL;

}

/*************************************** Rule.dll interface
***********************************/

int WINAPI Rule_NewSession(HWND hWnd,
RULECALLBACK lpfnCallback,
LPARAM lPar,
HRULE FAR * lphRule)
{
if (!lpfnRule_NewSession) return RULEERR_BADHANDLE;
return (*lpfnRule_NewSession)(hWnd,
lpfnCallback,
lPar,
lphRule);
}

int WINAPI Rule_EndSession(HRULE hRule)
{
if (!lpfnRule_EndSession) return RULEERR_BADHANDLE;
return (*lpfnRule_EndSession)(hRule);
}

int WINAPI Rule_ReadDiagnostics(HRULE hRule,LPDIAGNOSTICS lpDiag)
{
if (!lpfnRule_ReadDiagnostics) return RULEERR_BADHANDLE;
return (*lpfnRule_ReadDiagnostics)(hRule,lpDiag);

}

int WINAPI Rule_WriteDiagnostics(HRULE hRule,LPDIAGNOSTICS lpDiag)
{
if (!lpfnRule_WriteDiagnostics) return RULEERR_BADHANDLE;
return (*lpfnRule_WriteDiagnostics)(hRule,lpDiag);
}


//
************************************************** ***********************************
// CMyCEDlg.cpp: C++ application dialog

CMyCEDlg::CMyCEDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyCEDlg::IDD, pParent)
{

// HWND m_hWnd;
// HINSTANCE m_hInstRule;
// HRULE hRule = NULL;

/* Load Rule.dll */
m_hInstRule = LoadRule(_T("\\RULE.DLL"));
int iRet;
iRet = Rule_NewSession(m_hWnd, &MyCallback, (long)this,
&hRule);
if (iRet != RULEERR_OK)
{
UnloadRule(m_hInstRule);
m_hInstRule = NULL;
}
else
{ // load DLL success!
// do things ....
}

}

BOOL CALLBACK CMyCEDlg::CBRule(UINT nEvent, LPARAM lPar1, LPARAM
lPar2)
{
switch (nEvent)
{
case RULEEVENT_LINEOPENED:
//ShowMsgInfoText(_T("Line opened!"), (int) lPar1);
//bComStopped = FALSE;
break;

case RULEEVENT_LINECLOSED:
//ShowMsgInfoText(_T("Line closed!"), (int) lPar1);
break;

case RULEEVENT_CONNECTEDWITH:
//ShowMsgInfoText(_T("Connected with "), (int) lPar1);
break;

case RULEEVENT_OPERATIONFAILED:
//ShowMsgInfoText(_T("Operation failed!"), (int) lPar1);
break;

}

return FALSE;
}

BOOL CALLBACK MyCallback(UINT nEvent, LPARAM lPar1, LPARAM lPar2,
LPARAM dwExtra)
{

if (dwExtra)
{
CMyCEDlg* Owner = (CMyCEDlg*) dwExtra;
return Owner->CBRule(nEvent, lPar1, lPar2);
}
else
return FALSE;
}

Mar 8 '07 #1
2 7288
Any MVP and gurus from Microsoft? Please help!

This is a very hard task involving CALLBACK functions from C library
DLL, Interop, HandleRef, PInvoke, wrapper class, DllImport Marshaling,
etc....

How to convert Rule_NewSession() from C++ to C#?

int iRet;
iRet = Rule_NewSession(m_hWnd, &MyCallback, (long)this, &hRule);

int WINAPI Rule_NewSession(HWND hWnd,
RULECALLBACK lpfnCallback,
LPARAM lPar,
HRULE FAR * lphRule)

Thanks.

Mar 9 '07 #2

<My***********@gmail.comwrote in message
news:11*********************@j27g2000cwj.googlegro ups.com...
Any MVP and gurus from Microsoft? Please help!

This is a very hard task involving CALLBACK functions from C library
DLL, Interop, HandleRef, PInvoke, wrapper class, DllImport Marshaling,
etc....

How to convert Rule_NewSession() from C++ to C#?
Just don't. Take your existing C++ wrapper class, change class to public
ref class, and compile with /clr to create a C++/CLI assembly that calls the
C dll and is usable from any .NET language.

The initial conversion should work pretty much that easily. After that, you
may need to go through and rewrite any wrapper functions that work with
strings, to accept System::String^.

If you run into trouble compiling with /clr, try posting on the
microsoft.public.dotnet.languages.vc list
Mar 9 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Felix Kater | last post by:
Hi, in a given library I register callback functions with this function: bool set_callback(int index, int (*callback_function)(long)); I need the callback function to also pass the index...
3
by: John Smith | last post by:
I wrote some code in C in a dll which I would like to call from C#. However I'm stuck because of the strongly typed behavior of C# which makes limitations. Here are the prototypes for two...
4
by: Sai Kit Tong | last post by:
I have to interface managed application with my legacy dll. I have employed the wrapper approach but I have to deal with the asynchronous callback from the legacy dll, which likely goes through a...
2
by: joye | last post by:
Hello, My question is how to use C# to call the existing libraries containing unmanaged C++ classes directly, but not use C# or managed C++ wrappers unmanaged C++ classes? Does anyone know how...
7
by: Kirk McDonald | last post by:
Let's say I have a function that takes a callback function as a parameter, and uses it to describe an iteration: def func(callback): for i in : callback(i) For the sake of argument, assume...
7
by: harishashim | last post by:
I am wrapping a digital camera API using Managed C++ VS .NET 2003). I have this function that called as bellow in the API sample. err = PR_RC_StartViewFinder( m_hCamera, //line 1...
3
by: markb | last post by:
Hi My C# app is being called from a callback from an unmanaged DLL. One of the parameters of the callback is of type BOOL. I am using PInvoke to marshal this to a (managed) bool. The problem is...
6
by: HolyShea | last post by:
All, Not sure if this is possible or not - I've created a class which performs an asynchronous operation and provides notification when the operation is complete. I'd like the notification to be...
10
by: SQACPP | last post by:
Hi, I try to figure out how to use Callback procedure in a C++ form project The following code *work* perfectly on a console project #include "Windows.h" BOOL CALLBACK...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.