Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / March 2007

Tip: Looking for answers? Try searching our database.

Call C/C++ library DLL from C# with CALLBACK and messages

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
judy - 09 Mar 2007 14:41 GMT
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 or a
C# wrapper class so that I can call into the DLL from C# GUI. I need
help on how to convert the C++ code to C# calls.
How to convert:
int WINAPI Rule_NewSession(HWND hWnd,
                            RULECALLBACK lpfnCallback,
                            LPARAM lPar,
                            HRULE FAR * lphRule);

Thanks,

Judy

//
*************************************************************************************
//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;
}
Christian Fröschlin - 09 Mar 2007 15:39 GMT
> 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.

In this case, best forget about the C++ class wrapper and
write a C# class wrapper around the C DLL. The technology you
need to call plain C API methods from C# is called P/Invoke.
Basically, this involves declaring the function prototypes
for the C API methods in C# syntax similar to

[DllImport("rule")]
internal static extern void Test();

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.