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

non-COM DLL callback

Hi all,

I am new to .Net environment.
I have created a flat non-COM DLL from Visual C++ 6.0.
It stores up a function pointer from caller,
create a worker thread via WIN32 API,
and then call back the application within the worker thread.
Now, I created an application in VB.NET to use the DLL, and
it seems works fine.

I am not quite understand what is mananged and unmanaged code.
but seems that my DLL is called unmanaged code and
the code in VB is called managed code.
Is there any potential hazard in directly calling unmanaged DLL?
and also is it safe to call-back managed code from a thread in unmanaged
DLL?

What should I do in order to make it safe?

Here are some fragments of my code

In Application:

Module Module1

Public Declare Function RegisterEventHandler Lib "my.dll" _
Alias "_RegisterEventHandler@4" (ByVal lProcAddress As TEventHandler) _
As Integer

Delegate Sub TEventHandler(ByVal param As Integer)

Public Sub EventHandler(ByVal param As Integer)
Alter UI in Form1
.....
End Sub

End Module

Friend Class Form1
Inherits System.Windows.Forms.Form
Sub mySub()
RegisterEventHandler(AddressOf EventHandler)
End Sub
End Class

************************************************** *******

In DLL:

typedef void (CALLBACK* TEventHandler)(DWORD param);

TEventHandler EventHandler=NULL;
BOOL __declspec(dllexport) __stdcall RegisterEventHandler(long lProcAddress)
{
EventHandler = lProcAddress
hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&worker, NULL,
0, &pThreadId);

Return TRUE;
};

void worker(LPVOID lpParam)
{
....
loop{
EventHandler(param);
}
}
Nov 20 '05 #1
2 1811
On Thu, 29 Jul 2004 11:45:02 +0800, David wrote:
Hi all,

I am new to .Net environment.
I have created a flat non-COM DLL from Visual C++ 6.0.
It stores up a function pointer from caller,
create a worker thread via WIN32 API,
and then call back the application within the worker thread.
Now, I created an application in VB.NET to use the DLL, and
it seems works fine.

I am not quite understand what is mananged and unmanaged code.
but seems that my DLL is called unmanaged code and
the code in VB is called managed code.
Is there any potential hazard in directly calling unmanaged DLL?
and also is it safe to call-back managed code from a thread in unmanaged
DLL?

What should I do in order to make it safe?

Here are some fragments of my code

In Application:

Module Module1

Public Declare Function RegisterEventHandler Lib "my.dll" _
Alias "_RegisterEventHandler@4" (ByVal lProcAddress As TEventHandler) _
As Integer

Delegate Sub TEventHandler(ByVal param As Integer)

Public Sub EventHandler(ByVal param As Integer)
Alter UI in Form1
.....
End Sub

End Module

Friend Class Form1
Inherits System.Windows.Forms.Form
Sub mySub()
RegisterEventHandler(AddressOf EventHandler)
End Sub
End Class

************************************************** *******

In DLL:

typedef void (CALLBACK* TEventHandler)(DWORD param);

TEventHandler EventHandler=NULL;
BOOL __declspec(dllexport) __stdcall RegisterEventHandler(long lProcAddress)
{
EventHandler = lProcAddress
hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&worker, NULL,
0, &pThreadId);

Return TRUE;
};

void worker(LPVOID lpParam)
{
....
loop{
EventHandler(param);
}
}


A couple of things... It should be safe to call your callback from another
thread, but don't access the GUI from it. Windows forms are not thread
safe. If you need to update the UI, then look at the Control.Invoke (or
BeginInvoke) method. You can find out if a call to Invoke is necessary by
checking the InvokeRequired property:

' the method assumes your in a form and want to access gui elements
Private Sub CurrentMethod(sender As object, e as eventargs)
If Me.InvokeRequired Then
Dim d As EventHandler = AddressOf Me.CurrentMethod
Me.Invoke(d, new Object() {sender, e})
Else
' do stuff to alter the gui
end if
end sub

Obviously, the type of the delegate depends on your method :)

Another thing to keep in mind is make sure you keep a reference to your
callback that you sent to unmanged code around. If you don't and gc runs,
then it will clean up the delegate and the address will no longer be valid
and you will more then likely crash the runtime :)

Private d As mydelegatetype

private sub callthecallback()
me.d = addressof me.mycallbackfunction
theunmanagedfunction(d)
end sub

That's the only two things I can think of right now that might get you into
trouble if you're not careful...

--
Tom Shelton [MVP]
Nov 20 '05 #2
Tom Shelton wrote:
On Thu, 29 Jul 2004 11:45:02 +0800, David wrote:

Hi all,

I am new to .Net environment.
I have created a flat non-COM DLL from Visual C++ 6.0.
It stores up a function pointer from caller,
create a worker thread via WIN32 API,
and then call back the application within the worker thread.
Now, I created an application in VB.NET to use the DLL, and
it seems works fine.

I am not quite understand what is mananged and unmanaged code.
but seems that my DLL is called unmanaged code and
the code in VB is called managed code.
Is there any potential hazard in directly calling unmanaged DLL?
and also is it safe to call-back managed code from a thread in unmanaged
DLL?

What should I do in order to make it safe?

Here are some fragments of my code

In Application:

Module Module1

Public Declare Function RegisterEventHandler Lib "my.dll" _
Alias "_RegisterEventHandler@4" (ByVal lProcAddress As TEventHandler) _
As Integer

Delegate Sub TEventHandler(ByVal param As Integer)

Public Sub EventHandler(ByVal param As Integer)
Alter UI in Form1
.....
End Sub

End Module

Friend Class Form1
Inherits System.Windows.Forms.Form
Sub mySub()
RegisterEventHandler(AddressOf EventHandler)
End Sub
End Class

************************************************ *********

In DLL:

typedef void (CALLBACK* TEventHandler)(DWORD param);

TEventHandler EventHandler=NULL;
BOOL __declspec(dllexport) __stdcall RegisterEventHandler(long lProcAddress)
{
EventHandler = lProcAddress
hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&worker, NULL,
0, &pThreadId);

Return TRUE;
};

void worker(LPVOID lpParam)
{
....
loop{
EventHandler(param);
}
}

A couple of things... It should be safe to call your callback from another
thread, but don't access the GUI from it. Windows forms are not thread
safe. If you need to update the UI, then look at the Control.Invoke (or
BeginInvoke) method. You can find out if a call to Invoke is necessary by
checking the InvokeRequired property:

' the method assumes your in a form and want to access gui elements
Private Sub CurrentMethod(sender As object, e as eventargs)
If Me.InvokeRequired Then
Dim d As EventHandler = AddressOf Me.CurrentMethod
Me.Invoke(d, new Object() {sender, e})
Else
' do stuff to alter the gui
end if
end sub

Obviously, the type of the delegate depends on your method :)

Another thing to keep in mind is make sure you keep a reference to your
callback that you sent to unmanged code around. If you don't and gc runs,
then it will clean up the delegate and the address will no longer be valid
and you will more then likely crash the runtime :)

Private d As mydelegatetype

private sub callthecallback()
me.d = addressof me.mycallbackfunction
theunmanagedfunction(d)
end sub

That's the only two things I can think of right now that might get you into
trouble if you're not careful...


Thanks for your advice.

However, one of the UI in my form is a ListView
control(AxMSComctlLib.AxListView rather than
System.Windows.Forms.ListView. It was a upgraded project from VB6. I
don't know why the upgrade tool did such choice). Seems AxListView does
not provide InvokeRequired, What should I do?
I am sorry that I am not very good in VB6 too and don't know the
arhitecture of VB UI and multithreading.

Thanks.
Nov 20 '05 #3

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

Similar topics

5
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
3
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
25
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
22
by: Steve - DND | last post by:
We're currently doing some tests to determine the performance of static vs non-static functions, and we're coming up with some odd(in our opinion) results. We used a very simple setup. One class...
14
by: Patrick Kowalzick | last post by:
Dear all, I have an existing piece of code with a struct with some PODs. struct A { int x; int y; };
2
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
9
by: Francois Grieu | last post by:
When running the following code under MinGW, I get realloc(p,0) returned NULL Is that a non-conformance? TIA, Francois Grieu #include <stdio.h> #include <stdlib.h>
12
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
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
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
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,...

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.