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

shoehorn a managed object into an LPVOID

hello
I've got a C DLL that I've built which has got a callback function. Now I
want to call it from C#.

The method of using a delegate for the callback function works absolutely
beautifully.
However, the exported function of the Dll (that I PInvoke from c#) has an
LPVOID parameter for extradata, which it passes to the callback function.
How would I go about coding it if I wanted to pass a reference to a C# class
through this LPVOID? Do I need a HandleRef, and IntPtr, unsafe code?

Please enlighten me!

Thanks
Nov 16 '05 #1
2 3466
>However, the exported function of the Dll (that I PInvoke from c#) has an
LPVOID parameter for extradata, which it passes to the callback function.
How would I go about coding it if I wanted to pass a reference to a C# class
through this LPVOID? Do I need a HandleRef, and IntPtr, unsafe code?


Do you really have to use it? Since a delegate can refer to an
instance method, you canusually just store whatever state you need for
the callback as instance fields in the same class as the callback
method.

If you still want to use it, check out the GCHandle struct.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
Do you really have to use it? Since a delegate can refer to an
instance method, you canusually just store whatever state you need for
the callback as instance fields in the same class as the callback
method.
Ah - that's an option I suppose...

If you still want to use it, check out the GCHandle struct.
I have done. THe only question is, the GC won't "sweep it away" while the
unmanaged code is running will it?

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 16 '05 #3

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

Similar topics

2
by: Krish | last post by:
Hi, I have a question, if I have a global function that pointed to class or object, then how do I pass arguments to that function. I have CService *MyService; in my cpp and in my header, i have...
2
by: greyham | last post by:
Hi. I'm a beginner to C++ so bear with me. I'm trying to pass a character array to a new thread so I thought I could pass the pointer to the character array and cast it as a LPVOID (I know nothing...
2
by: Herbert VON GRÜNENWALD | last post by:
i'v got: public static extern int ReadFile( IntPtr hFile, IntPtr lpBuffer, uint nNumberOfBytesToRead, ref uint lpNumberOfBytesRead, uint lpOverlapped);
3
by: Gustavo L. Fabro | last post by:
Greetings! I'm a newbie in Visual C++ .NET (have programmed in Borland C++ and Builder for long) and I am trying to do a very simple thing, but I'm stuck. I created an (unmanaged) DLL project...
4
by: Dan Diephouse | last post by:
I am attempting to write a DLL which wraps C# code. The issue at hand is a print driver allows you to extend it by writing a DLL. Seeing that VC++ seems to be hopelessly out of my mental reach, I...
7
by: Russell Mangel | last post by:
I have been doing some C++ Interop using the new VS2005 (June Beta). I am exposing these methods to .NET clients. I ran into some WinAPI methods which use LPVOID types, and I don't understand...
7
by: Toni | last post by:
Hello Everyone , I'm trying to convert an LPCSTR to LPVOID for use in DeviceIOControl. But when i use LPVOID test = (LPVOID)test_lpcstr then it doesn't work , When i use LPVOID test = "TEST" ,...
0
by: micho3 | last post by:
Here is my problem! I'm trying to use a Unmanaged dll (Use of MFC) into a C# project. But I know I have to wrap it before I can use it! So I've tried to wrap it without any success ! I'm new in this...
5
by: Ami | last post by:
Hi All, I am trying to create a class whose constructor takes Void parameter and stores it for future reference. It works fine as expected. class base { public: base(LPVOID pObj){ //store...
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:
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,...
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...

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.