473,386 Members | 1,799 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,386 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 1768
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
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...

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.