473,404 Members | 2,137 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,404 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 1744
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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,...
0
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...

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.