473,325 Members | 2,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Call back function and VC 2005


I have a DLL written in C++ (unmanaged), which I want to inteface with.

I created a standard window form program with VC++ 2005, all fine
I have imported an .h file the .lib file and all works.

/clr is on so I am writing managed code.

the problem comes when I need to use a function of the DLL with a callback
function parameter. (address of a function)

No way to get it compiled due to managed code restrictions.

I can get it working with /clr:noAssembly but i'd rather use managed code.

I think that I need to use delegate? correct?

I did not found any example, anyone can help me?

Many Thanks,

Filippo

Sep 1 '06 #1
5 1742
MS has some decent examples of how to do this. See data marshaling
between unmanaged/managed apps:
http://msdn.microsoft.com/library/de...backsample.asp

hope this helps,
Sharon

Filippo Bettinaglio wrote:
I have a DLL written in C++ (unmanaged), which I want to inteface with.

I created a standard window form program with VC++ 2005, all fine
I have imported an .h file the .lib file and all works.

/clr is on so I am writing managed code.

the problem comes when I need to use a function of the DLL with a callback
function parameter. (address of a function)

No way to get it compiled due to managed code restrictions.

I can get it working with /clr:noAssembly but i'd rather use managed code.

I think that I need to use delegate? correct?

I did not found any example, anyone can help me?

Many Thanks,

Filippo
Sep 1 '06 #2

I was looking for something in VC++ 2005 not C# or VB

Thanks,
Filippo
"dotnetchic" wrote:
MS has some decent examples of how to do this. See data marshaling
between unmanaged/managed apps:
http://msdn.microsoft.com/library/de...backsample.asp

hope this helps,
Sharon

Filippo Bettinaglio wrote:
I have a DLL written in C++ (unmanaged), which I want to inteface with.

I created a standard window form program with VC++ 2005, all fine
I have imported an .h file the .lib file and all works.

/clr is on so I am writing managed code.

the problem comes when I need to use a function of the DLL with a callback
function parameter. (address of a function)

No way to get it compiled due to managed code restrictions.

I can get it working with /clr:noAssembly but i'd rather use managed code.

I think that I need to use delegate? correct?

I did not found any example, anyone can help me?

Many Thanks,

Filippo

Sep 1 '06 #3
Check...
http://www.developer.com/net/cplus/article.php/3510471 and
http://www.codeproject.com/managedcpp/delegates.asp

Cheers!!
-Shekhar

"Filippo Bettinaglio" <Fi****************@discussions.microsoft.comwro te
in message news:48**********************************@microsof t.com...
>
I was looking for something in VC++ 2005 not C# or VB

Thanks,
Filippo
"dotnetchic" wrote:
>MS has some decent examples of how to do this. See data marshaling
between unmanaged/managed apps:
http://msdn.microsoft.com/library/de...backsample.asp

hope this helps,
Sharon

Filippo Bettinaglio wrote:
I have a DLL written in C++ (unmanaged), which I want to inteface with.

I created a standard window form program with VC++ 2005, all fine
I have imported an .h file the .lib file and all works.

/clr is on so I am writing managed code.

the problem comes when I need to use a function of the DLL with a
callback
function parameter. (address of a function)

No way to get it compiled due to managed code restrictions.

I can get it working with /clr:noAssembly but i'd rather use managed
code.

I think that I need to use delegate? correct?

I did not found any example, anyone can help me?

Many Thanks,

Filippo


Sep 1 '06 #4
Hi Filippo!
I have a DLL written in C++ (unmanaged), which I want to inteface with.

I created a standard window form program with VC++ 2005, all fine
I have imported an .h file the .lib file and all works.

/clr is on so I am writing managed code.

the problem comes when I need to use a function of the DLL with a callback
function parameter. (address of a function)

No way to get it compiled due to managed code restrictions.
?

There are two ways:

1. Using Explicit PInvoke
http://msdn2.microsoft.com/en-us/library/eyzhw3s8.aspx

Here is a small example for WinAPI "EnumWindows":

delegate int EnumWindowsDelegate2(int hWnd, int lParam);

[System::Runtime::InteropServices::DllImportAttribu te("user32.dll",
EntryPoint="EnumWindows")]
static int MyEnumWindows(EnumWindowsDelegate2^ callback, int lParam);

void button2_Click(System::Object^ sender, System::EventArgs^ e)
{
sb = gcnew System::Text::StringBuilder();
MyEnumWindows(gcnew EnumWindowsDelegate2(this, &Form1::MyCallback2), 0);
MessageBox::Show(sb->ToString());
}
System::Text::StringBuilder ^sb;
int MyCallback2(int hWnd, int lParam)
{
sb->AppendFormat("{0}\r\n", hWnd);
return 1;
}
2. More sophisticated way with a struct (but needs to be compiled with
/clr only!):
(Same example as above):

delegate bool EnumWindowsDelegate1(HWND hWnd);
struct EnumWindowsCallback
{
EnumWindowsCallback(EnumWindowsDelegate1 ^d) : del(d) {}
gcroot<EnumWindowsDelegate1^del;
bool Execute()
{
EnumWindows(&MyEnumWindowsCallback, (LPARAM) this);
return true;
}
static BOOL CALLBACK MyEnumWindowsCallback(HWND hWnd, LPARAM lParam)
{
EnumWindowsCallback *pThis = (EnumWindowsCallback*) lParam;
bool bRet = pThis->del->Invoke(hWnd);
if (bRet == true)
return TRUE;
return FALSE;
}
};

void button1_Click(Object^ sender, EventArgs^ e)
{
sb = gcnew System::Text::StringBuilder();
EnumWindowsCallback m(gcnew EnumWindowsDelegate1(this,
&Form1::MyCallback1));
m.Execute();
MessageBox::Show(sb->ToString());
}

System::Text::StringBuilder ^sb;
bool MyCallback1(HWND hWnd)
{
int i = (int) hWnd;
sb->AppendFormat("{0}\r\n", i);
return true;
}

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Sep 1 '06 #5
I got the same problem last week... and finally decided to get the
information from MSDN. Miracle I found the info... Not exactly where
expected... But I found it, if you know that you need to use the Interop
namespace it can help to find out the information, but I don't know exactly
where I found the info. Anyway That piece of code should work... Important
thing is the GetFunctionPointerForDelegate, it returns a pointer that is not
locked in the GC. You need to use the GCHandle::Alloc function to lock that
delegate :-)

// Delegate Declaration
delegate prResponse CamCallBackFuncDelegate(prHandle CameraHandle,prContext
Context,prVoid* pEventData);

// Your Code

CamCallBackFuncDelegate ^del = gcnew CamCallBackFuncDelegate(this,
&CanonSDK::RemoteCapture::CamCallBackFunc);

// Locks the delegate from the GC ?

GCHandle gch = GCHandle::Alloc(del);

// Get Pointer and SET unmanaged callback function

IntPtr ip = Marshal::GetFunctionPointerForDelegate(del);
prSetEventCB* cb = static_cast<prSetEventCB *>(ip.ToPointer());

// Callback setup

cErr = PR_SetEventCallBack( m_hCamera,(prContext) 1234,(prSetEventCB*) cb );

"Filippo Bettinaglio" <Fi****************@discussions.microsoft.comwro te
in message news:A8**********************************@microsof t.com...
>
I have a DLL written in C++ (unmanaged), which I want to inteface with.

I created a standard window form program with VC++ 2005, all fine
I have imported an .h file the .lib file and all works.

/clr is on so I am writing managed code.

the problem comes when I need to use a function of the DLL with a callback
function parameter. (address of a function)

No way to get it compiled due to managed code restrictions.

I can get it working with /clr:noAssembly but i'd rather use managed code.

I think that I need to use delegate? correct?

I did not found any example, anyone can help me?

Many Thanks,

Filippo

Sep 18 '06 #6

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

Similar topics

10
by: Carramba | last post by:
hi! I don't have eny code to chare, this is just teoretical question. usualy you have for looks like <form method="POST" action="some_php_script.php"> namn: <input type="text" name="name"...
39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
12
by: windandwaves | last post by:
Hi Gurus When I have a query in which I use a small function, e.g.: SELECT A03_FILES.ID, A03_FILES.D, hasvt() AS hsvVT FROM A03_FILES; where HasVT is defined below: --------------------
13
by: Larry Menard | last post by:
Test code: $dbconn = odbc_connect($dbname, $username, $password); $path = "C:\Temp\myJar.jar"; $statement = "CALL SQLJ.INSTALL_JAR('file://$path', 'myJarId')"; $result = odbc_exec($dbconn,...
3
by: Marshall | last post by:
Hello, I am wondering if it is possible to call a remote web service using ATLAS and if so, how. I have read several docs which show how to call a web service that is within the same project as...
0
by: Filippo Bettinaglio | last post by:
I have a DLL written in C++ (unmanaged), which I want to inteface with. I created a standard window form program with VC++ 2005, all fine I have imported an .h file the .lib file and all works. ...
2
by: Zytan | last post by:
How can I show the function call stack window in VB 2005 Express? Debugging is rather hard without it... Thanks! Zytan
4
by: Ty | last post by:
Hello all, I am creating a web site with Visual Stuido 2008. I am trying to use a java script file to create a busybox for login from this page http://blogs.crsw.com/mark/articles/642.aspx. I...
0
by: tomb | last post by:
Can anybody help me by checking if I have correct call signatures? In my C# Windows Form project (VS2008) I am using dll which has been written for Visual C++ 6.0: This function code is used to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.