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;
}