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

Passing 'this' to unmanaged code

I am trying to call an unmanaged dll from c#, the function is asyncronous and expects a callback function and a parameter which is labelled lpvoid but which expects the address of the calling object, which is then returned by the callback function. Using a delegate the call back function works perfectly and I can pass value objects as parameters, but a not reference object, is it possible and if so how do you do it.

PS I had hoped to get away with ignoring the parameter, as I expected the delegate to return to callback function of the object that called it. But when used from muliple objects simultaneously, all the callbacks are to the last object that calls the function. In other words, the unmanaged code always calls back on the last function given it.
Nov 16 '05 #1
2 1341
Hi,

Unfortunately what you want to do is not valid, if I remember correctly, "this" is not an actual object variable and hence cannot be passed as reference. this is actually a keyword, which does not allow values being set to it.

This is probably not the answer you want. if you really want, you can just create another object variable, set it's reference to "this" and pass that by ref, but if you do get a new object instance returned, you cannot replace the context that "this" represents..
Cheers

Eddie de Bear
--
Eddie de Bear
MCSD
"tony lock" wrote:
I am trying to call an unmanaged dll from c#, the function is asyncronous and expects a callback function and a parameter which is labelled lpvoid but which expects the address of the calling object, which is then returned by the callback function. Using a delegate the call back function works perfectly and I can pass value objects as parameters, but a not reference object, is it possible and if so how do you do it.

PS I had hoped to get away with ignoring the parameter, as I expected the delegate to return to callback function of the object that called it. But when used from muliple objects simultaneously, all the callbacks are to the last object that calls the function. In other words, the unmanaged code always calls back on the last function given it.

Nov 16 '05 #2
Using a delegate the call back function works perfectly and I can pass value objects as parameters, but a not reference object, is it possible and if so how do you do it.


You can use the GCHandle struct to get a handle to an object. The
handle can be passed to/from unmanaged code and turned back into an
object reference.

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

1
by: lolomgwtf | last post by:
I have a managed C++ method that wraps unmanaged code and creates a managed object holding data retrieved form an unmanged one. I want create an instance of this managed class in C#, pass it to...
2
by: lolomgwtf | last post by:
I have a managed C++ method that wraps unmanaged code and creates a managed object holding data retrieved form an unmanged one. I want create an instance of this managed class in C#, pass it to...
1
by: Steve Baer | last post by:
I'm wrapping some unmanaged C++ classes with managed versions for use in the ..NET world. Everything is going great except I can't figure out a good method for passing simple data type arrays into...
2
by: Sasha Nikolic | last post by:
I have one unmanaged and one managed class in the same vc++ project. Managed object creates one instance of unmanaged class and needs to pass a callback reference so that unmanaged class can...
17
by: mr.resistor | last post by:
hey i am having a few problems calling a C DLL from C#. i am using a simple function that takes an array of floats and an integer as an input, but i cannot seem to get it to work. when i try to...
0
by: Haxan | last post by:
Hi, I have an unmanaged application that converts a function pointer to a delegate and then pass this as a parameter(delegate) to a managed function which then invokes it. Currently Im able to...
12
by: Haxan | last post by:
Hi, I have my main application class(unmanaged) that has a none static member function that I need to pass as a delegate to managed C# method. In one of the methods of this class(unmamanged),...
9
by: =?Utf-8?B?U2hhcm9u?= | last post by:
In my managed code (C#) I have a some dozens of single dimension byte array. I get each of this arrays from another unmanaged code (first DLL). I need to stitch these arrays to a dual dimensions...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
0
by: mjaaland | last post by:
Hi! I've been working with DLLimports passing structs and various other parameters to unmanaged code. I had problems earlier sending pointer to structs to the unmanaged code, and this forum solved...
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: 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:
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
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
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.