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! :-(