how to write a callback function to be invoked by C++ DLL?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi Wen,

Greetings form GarbageCollection!!!

you got in trouble cause of you lost the instance of your delegate (it's
only in your Form_Load).
public delegate void delegateCH(string szInfo);

It's not an instance member, but a type declaration somewhat like this (the
compiler internally generates a type!!!)

public class delegateCH : Delegate{...}


So introduce in your Form a member of the delegate

partial class Form1 : Form
{

pivate static delegateCH _delegate_Callback_Handler = null;

private void Form1_Load(object sender, EventArgs e)
{
....

_delegate_Callback_Handler = new delegateCH(ImageConv_Callback_Handler);

SetImageConvCallbackFunc(_delegate_Callback_Handler);

....
}

I think it should work

Roland
 
hello,
now, i wanna port a c++ program into C# project. a DLL written by C++ and
keep it no change, and UI wirtten by C#.

my C++ UI code is as below:

// error handlers --global function callback function
// C++ dll would invoke this function if needed
void ImageConv_Callback_Handler(const char *szInfo)
{
AfxMessageBox(szInfo);
}

CXXXDlg::OnInitDialog(...)
{
....
// Set Callback Function
SetImageConvCallbackFunc(ImageConv_Callback_Handler);
....
}

{ // work flow
.....
}

and it works fine.

i wrote C# code for the same function, so that i need 2 write a callback
function in C#. my C# code is as below:

namespace ImageConvTest
{

partial class Form1 : Form
{
.....
public delegate void delegateCH(string szInfo);

private void ImageConv_Callback_Handler(string szInfo)
{
MessageBox.Show(szInfo);
}

// error functions
[DllImport("ImageConv.dll", EntryPoint = "SetImageConvCallbackFunc")]
public static extern void
SetImageConvCallbackFunc(delegateCHpFuncCallbackProc);// in this case, is
delegateCHcorrect? is it equal to "void *"???

private void Form1_Load(object sender, EventArgs e)
{
InitImageConvLibrary();
//
delegateCH delegate_Callback_Handler = new
delegateCH(ImageConv_Callback_Handler);
SetImageConvCallbackFunc(delegate_Callback_Handler);
}

here, strange things happened to the C# project.
compiled it and got no errors. run it with no errors too. once i trigger the
function which is in C++ DLL, runtime exception occurs :-(

and if comments //SetImageConvCallbackFunc(delegate_Callback_Handler);
the program works fine.


is there any method to solve this problem? thank u very much.
 
Wen said:
yes, i did and it works now, but when i trigger the function which is
implemented in C++ dll, the app will disappear :-(

these are APIs of my C++ DLL:
// Init & DeInit functions
IMAGECONV_API
void DLL_CALLCONV InitImageConvLibrary();
IMAGECONV_API
void DLL_CALLCONV DeInitImageConvLibrary();

// error functions
typedef void (*IMAGECONV_CALLBACK)(const char *);
IMAGECONV_API

How is IMAGECONV_CALLBACK defined (typedef)?

Willy.
 
Hi Wen,

Greetings form GarbageCollection!!!

you got in trouble cause of you lost the instance of your delegate (it's
only in your Form_Load).


It's not an instance member, but a type declaration somewhat like this (the
compiler internally generates a type!!!)

public class delegateCH : Delegate{...}


So introduce in your Form a member of the delegate

partial class Form1 : Form
{

pivate static delegateCH _delegate_Callback_Handler = null;


...

_delegate_Callback_Handler = new delegateCH(ImageConv_Callback_Handler);

SetImageConvCallbackFunc(_delegate_Callback_Handler);

...
}

I think it should work

Roland

yes, i did and it works now, but when i trigger the function which is
implemented in C++ dll, the app will disappear :-(

these are APIs of my C++ DLL:
// Init & DeInit functions
IMAGECONV_API
void DLL_CALLCONV InitImageConvLibrary();
IMAGECONV_API
void DLL_CALLCONV DeInitImageConvLibrary();

// error functions
typedef void (*IMAGECONV_CALLBACK)(const char *);
IMAGECONV_API

//void DLL_CALLCONV SetImageConvCallbackFunc(void *pFuncCallbackProc);
void DLL_CALLCONV SetImageConvCallbackFunc(IMAGECONV_CALLBACK
pFuncCallbackProc);

// do convert images functions

IMAGECONV_API
bool DLL_CALLCONV BMPtoGIF(int phoneType, const char *in_filename, const
char *out_filename);
IMAGECONV_API
bool DLL_CALLCONV BMPtoPNG(int phoneType, const char *in_filename, const
char *out_filename);

.....


in C# project, the related code is as following:
partial class Form1 : Form
{
... ...
public delegate void delegateCH(string szInfo);

public void ImageConv_Callback_Handler(string szInfo)
{
MessageBox.Show(szInfo);
}

private static delegateCH _delegate_Callback_Handler = null;

// Init & Deinit
[DllImport("ImageConv.dll", EntryPoint = "InitImageConvLibrary")]
public static extern void InitImageConvLibrary();

[DllImport("ImageConv.dll", EntryPoint = "DeInitImageConvLibrary")]
public static extern void DeInitImageConvLibrary();

// error functions
[DllImport("ImageConv.dll", EntryPoint =
"SetImageConvCallbackFunc")]
public static extern void SetImageConvCallbackFunc(delegateCH
pFuncCallbackProc);

// do convert images functions
[DllImport("ImageConv.dll", EntryPoint = "BMPtoGIF")]
public static extern bool BMPtoGIF(int phoneType, string
in_filename, string out_filename);

[DllImport("ImageConv.dll", EntryPoint = "BMPtoPNG")]
public static extern bool BMPtoPNG(int phoneType, string
in_filename, string out_filename);

.... ....

private void Form1_Load(object sender, EventArgs e)
{
InitImageConvLibrary();
//
_delegate_Callback_Handler = new
delegateCH(ImageConv_Callback_Handler);
SetImageConvCallbackFunc(_delegate_Callback_Handler); // i must
comment this sentence to make the app run properly, and i cannot use
callback function to support more details for users :-(
}

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
DeInitImageConvLibrary();
}

private void ImageConvert_Click(object sender, EventArgs e)
{
....
if (strFileExtension.CompareTo(".BMP") == 0)
{
BMPtoPNG(m_nPhoneTypeConversation, strSrcPath,
strDestPath);
}
else if (strFileExtension.CompareTo(".JPG") == 0)
{
JPEGtoPNG(m_nPhoneTypeConversation, strSrcPath,
strDestPath);
}
...
}


}
}


very strange! :-(
 
Back
Top