473,698 Members | 2,360 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Wen
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_Callb ack_Handler(con st char *szInfo)
{
AfxMessageBox(s zInfo);
}

CXXXDlg::OnInit Dialog(...)
{
....
// Set Callback Function
SetImageConvCal lbackFunc(Image Conv_Callback_H andler);
....
}

{ // 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(stri ng szInfo);

private void ImageConv_Callb ack_Handler(str ing szInfo)
{
MessageBox.Show (szInfo);
}

// error functions
[DllImport("Imag eConv.dll", EntryPoint = "SetImageConvCa llbackFunc")]
public static extern void
SetImageConvCal lbackFunc(deleg ateCHpFuncCallb ackProc);// in this case, is
delegateCHcorre ct? is it equal to "void *"???

private void Form1_Load(obje ct sender, EventArgs e)
{
InitImageConvLi brary();
//
delegateCH delegate_Callba ck_Handler = new
delegateCH(Imag eConv_Callback_ Handler);
SetImageConvCal lbackFunc(deleg ate_Callback_Ha ndler);
}

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 //SetImageConvCal lbackFunc(deleg ate_Callback_Ha ndler);
the program works fine.
is there any method to solve this problem? thank u very much.

--
With my kind regards,
Wen
Nov 16 '05 #1
3 1954
Hi Wen,

Greetings form GarbageCollecti on!!!

you got in trouble cause of you lost the instance of your delegate (it's
only in your Form_Load).
public delegate void delegateCH(stri ng 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_Callb ack_Handler = null;

private void Form1_Load(obje ct sender, EventArgs e)
{

....

_delegate_Callb ack_Handler = new delegateCH(Imag eConv_Callback_ Handler);

SetImageConvCal lbackFunc(_dele gate_Callback_H andler);

....
}

I think it should work

Roland
Nov 16 '05 #2
Wen
Hi Wen,

Greetings form GarbageCollecti on!!!

you got in trouble cause of you lost the instance of your delegate (it's
only in your Form_Load).
public delegate void delegateCH(stri ng 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_Callb ack_Handler = null;

private void Form1_Load(obje ct sender, EventArgs e)
{

...

_delegate_Callb ack_Handler = new delegateCH(Imag eConv_Callback_ Handler);

SetImageConvCal lbackFunc(_dele gate_Callback_H andler);

...
}

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 InitImageConvLi brary();
IMAGECONV_API
void DLL_CALLCONV DeInitImageConv Library();

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

//void DLL_CALLCONV SetImageConvCal lbackFunc(void *pFuncCallbackP roc);
void DLL_CALLCONV SetImageConvCal lbackFunc(IMAGE CONV_CALLBACK
pFuncCallbackPr oc);

// 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(stri ng szInfo);

public void ImageConv_Callb ack_Handler(str ing szInfo)
{
MessageBox.Show (szInfo);
}

private static delegateCH _delegate_Callb ack_Handler = null;

// Init & Deinit
[DllImport("Imag eConv.dll", EntryPoint = "InitImageConvL ibrary")]
public static extern void InitImageConvLi brary();

[DllImport("Imag eConv.dll", EntryPoint = "DeInitImageCon vLibrary")]
public static extern void DeInitImageConv Library();

// error functions
[DllImport("Imag eConv.dll", EntryPoint =
"SetImageConvCa llbackFunc")]
public static extern void SetImageConvCal lbackFunc(deleg ateCH
pFuncCallbackPr oc);

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

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

.... ....

private void Form1_Load(obje ct sender, EventArgs e)
{
InitImageConvLi brary();
//
_delegate_Callb ack_Handler = new
delegateCH(Imag eConv_Callback_ Handler);
SetImageConvCal lbackFunc(_dele gate_Callback_H andler); // 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_FormClose d(object sender, FormClosedEvent Args e)
{
DeInitImageConv Library();
}

private void ImageConvert_Cl ick(object sender, EventArgs e)
{
....
if (strFileExtensi on.CompareTo(". BMP") == 0)
{
BMPtoPNG(m_nPho neTypeConversat ion, strSrcPath,
strDestPath);
}
else if (strFileExtensi on.CompareTo(". JPG") == 0)
{
JPEGtoPNG(m_nPh oneTypeConversa tion, strSrcPath,
strDestPath);
}
...
}
}
}
very strange! :-(
Nov 16 '05 #3

"Wen" <we************ @hotmail.com> wrote in message
news:eC******** ******@TK2MSFTN GP12.phx.gbl...
Hi Wen,

Greetings form GarbageCollecti on!!!

you got in trouble cause of you lost the instance of your delegate (it's
only in your Form_Load).
> public delegate void delegateCH(stri ng 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_Callb ack_Handler = null;

> private void Form1_Load(obje ct sender, EventArgs e)
> {

...

_delegate_Callb ack_Handler = new delegateCH(Imag eConv_Callback_ Handler);

SetImageConvCal lbackFunc(_dele gate_Callback_H andler);

...
}

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 InitImageConvLi brary();
IMAGECONV_API
void DLL_CALLCONV DeInitImageConv Library();

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


How is IMAGECONV_CALLB ACK defined (typedef)?

Willy.
Nov 16 '05 #4

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

Similar topics

3
6329
by: ThinkRS232 | last post by:
I have a Win32 DLL that has a standard _stdcall (WINAPI) exports. I am able to call these fine from C#. One call in particular however has a callback to a CDECL function. How would I set that up? Following is the specific. Win32 DLL Declaration for function in MyDLL.dll extern "C" int WINAPI SpecialTimerFunction(int Val, int (*Callback)(int InVal)) C# Declaration public class MyClass
8
572
by: kurtcobain1978 | last post by:
-------------------------------------------------------------------------------- I need to do the exactly same thing in VB.NET. Load a unmanaged C DLL dynamically and then call a function in which I pass the callback function as an argument. My C function being called callback as type _cdecl. Does anybody have any ideas?
10
6979
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 MyEnumWindowsProc(HWND hwnd, LPARAM lparam) {
1
2415
by: kikivenkat | last post by:
Hi, I understand the concept of function pointers. I am a little confused with the call back functions. 1.If a function is invoked using a function pointer, then does it mean the function invoked is a callback function? 2. Some say that the calling convention used for a callback function is different(__stdcall,PASCAL).However, I see that such calling conventions are used only for callback functions registered with the OS.I dont see...
0
8678
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7737
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6525
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4621
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2333
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.