473,396 Members | 1,963 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,396 software developers and data experts.

manged callback from unamaged dll

MR
help!

I have an unmanaged DLL that I do not have the source code, so i can't
recompile or make changes. the DLL requires a callback function. I would
like to implement the callback method in a managed class. The unmanaged DLL
gets the address of the callback in a struct (someStruct below) that is
passed as a parameter. I can't seem to figure out how to pass the
"unmanaged" address of a managed method to be uses as a callback.

I would appreciate some guidance here

thanks very much

(i am using VS C++ 2005)

// Initialize the callback structure for the Speak&Find object

ref class managedClass

{

SOMESTRUCT someStruct;

someStruct.onResultCallback = &onResult; // HOW DO I ASSIGN THE ADDRESS of
onResult

UnmanagedDLL.Init(&someStruct);

void managedClass::onResult (int, char*, long) // callback for some
unmanaged DLL

{

// some code here

}

}
Nov 17 '05 #1
2 1770
Hi,
... I can't seem to figure out how to pass the "unmanaged"
address of a managed method to be uses as a callback.


yes, you cannot call a .NET function from a unmanaged dll, since there is
no function address for a managed method, it should be a delegate instead,
but you could not pass a delegate to the unmanaged DLL directly, a .NET
type is meaningless to the unmanaged environment.

So in this scenario, I suggest you take the approach which applied in
VS2003, use an unmanaged helper function to call the managed method
instead, then pass this unmanaged function's address to the SOMESTRUCT
struct, please refer to the following CodeProject article for related
sample code:

Calling Managed .NET Function from Unmanaged Windows Custom DLL.
http://www.codeproject.com/csharp/Win32_to_NET.asp
Thanks!

Best regards,

Gary Chang
Microsoft Community Support
--------------------
Get Secure! ˇ§C www.microsoft.com/security
Register to Access MSDN Managed Newsgroups!
http://support.microsoft.com/default...sdn/nospam.asp
&SD=msdn

This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 17 '05 #2
Hi,

I think the following code shows how to do this, you might want to pin the
callback before the assignment
to the struct however. Don't be put off by the fact that you don't have
access to the dll code, all you have to do
is construct an equivalant managed struct as I have. I didn't think it was
essential to do it with un-managed
code as suggested by a previous post.

Hope it's not too late.

Paul.

[----------------Start-------------------------]
// This is the main project file for VC++ application project
// generated using an Application Wizard.

#include "stdafx.h"

#using <mscorlib.dll>
#include <tchar.h>

using namespace System;
using namespace System::Runtime::InteropServices;

__delegate bool CallBack(int hwnd, int lParam); // a delegate type

//model this struct exactly like the one passed to your dll function
[StructLayout(LayoutKind::Sequential)]
public __gc struct DllCallBackStruct
{
//this tells the marshaler the delegate is a function ptr
[MarshalAs(UnmanagedType::FunctionPtr)]
CallBack* pcbFunc;
//any other data ...
System::Int32 iData;
};

__gc class EnumReport // managed type with the method to call
{
public:
bool Report(int hwnd, int lParam) { // report the window handle
Console::Write(L"Window handle is ");
Console::WriteLine(hwnd);
return true;
}
};

[DllImport("CallBackModule.dll", EntryPoint = "fnCallEnumWindows", CharSet =
Unicode)]
extern "C" void fnCallEnumWindows(DllCallBackStruct* lpData);

int _tmain(void) {

//create class with callback member
EnumReport* er = new EnumReport;
//create call back object
CallBack* myCallBack = new CallBack(er, &EnumReport::Report);
//create struct that holds callback function
DllCallBackStruct* lpStruct = new DllCallBackStruct();
//assign callback function to struct
lpStruct->pcbFunc = myCallBack;
//call dll
fnCallEnumWindows(lpStruct);

return 0;
}

/////////////////////////////////////////////////Dll Code used to test
idea///////////////////////////////////////////////

struct CALLBACKMODULE_API DllCallBackStruct {
WNDENUMPROC lpcbFunc;
long lData;
};

extern "C" CALLBACKMODULE_API void fnCallEnumWindows(DllCallBackStruct*
lpData);
CALLBACKMODULE_API void fnCallEnumWindows(DllCallBackStruct* lpData)
{
EnumWindows(lpData->lpcbFunc, 99);
}

[----------------Finsih------------------------]

"MR" <co******@newsgroup.nospam> wrote in message
news:O3**************@TK2MSFTNGP10.phx.gbl...
help!

I have an unmanaged DLL that I do not have the source code, so i can't
recompile or make changes. the DLL requires a callback function. I would
like to implement the callback method in a managed class. The unmanaged
DLL gets the address of the callback in a struct (someStruct below) that
is passed as a parameter. I can't seem to figure out how to pass the
"unmanaged" address of a managed method to be uses as a callback.

I would appreciate some guidance here

thanks very much

(i am using VS C++ 2005)

// Initialize the callback structure for the Speak&Find object

ref class managedClass

{

SOMESTRUCT someStruct;

someStruct.onResultCallback = &onResult; // HOW DO I ASSIGN THE ADDRESS
of onResult

UnmanagedDLL.Init(&someStruct);

void managedClass::onResult (int, char*, long) // callback for some
unmanaged DLL

{

// some code here

}

}

Nov 17 '05 #3

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

Similar topics

1
by: scott ocamb | last post by:
hello I have implemented a solution using async methods. There is one async method that can be invoked multiple times, ie there are multiple async "threads" running at a time. When these...
4
by: ma740988 | last post by:
// file sltest.h #ifndef SLTEST_H #define SLTEST_H class CallbackBase // herb shutters gotW source .. { public: virtual void operator()() const { }; virtual ~CallbackBase() = 0; };
15
by: Felix Kater | last post by:
Hi, in a given library I register callback functions with this function: bool set_callback(int index, int (*callback_function)(long)); I need the callback function to also pass the index...
0
by: P Reddy | last post by:
Hi All, Greetings!!! I have a question. Please respond.... I am trying to write a C# component that need to inherit some of the the interfaces from unmanged code(VC). I think one way to...
4
by: gabe | last post by:
i have a solution to gather the text in each window that is open, but would rather use only managed code rather than unmanaged code my current solution relies on. The purpose is to get the text in...
6
by: smmk25 | last post by:
Before I state the problem, I just want to let the readers know, I am knew to C++\CLI and interop so please forgive any newbie questions. I have a huge C library which I want to be able to use in...
10
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...
0
by: Tim Spens | last post by:
--- On Fri, 6/27/08, Tim Spens <t_spens@yahoo.comwrote: I think I know where the problem is but I'm unsure how to fix it. When I call Register_Handler(...) from python via...
5
by: Jef Driesen | last post by:
I have a C DLL that I want to use from a C# project. The C header file contains these declarations: typedef void (*callback_t) (const unsigned char *data, unsigned int size, void *userdata);...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
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...
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,...

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.