Click here to Skip to main content
15,892,161 members
Home / Discussions / C#
   

C#

 
AnswerRe: Marshaling between managed and unmanaged Pin
Luc Pattyn28-Sep-09 7:45
sitebuilderLuc Pattyn28-Sep-09 7:45 
GeneralRe: Marshaling between managed and unmanaged Pin
akhanal28-Sep-09 8:28
akhanal28-Sep-09 8:28 
GeneralRe: Marshaling between managed and unmanaged Pin
Luc Pattyn28-Sep-09 8:48
sitebuilderLuc Pattyn28-Sep-09 8:48 
GeneralRe: Marshaling between managed and unmanaged Pin
Richard MacCutchan28-Sep-09 9:16
mveRichard MacCutchan28-Sep-09 9:16 
GeneralRe: Marshaling between managed and unmanaged Pin
Luc Pattyn28-Sep-09 9:32
sitebuilderLuc Pattyn28-Sep-09 9:32 
GeneralRe: Marshaling between managed and unmanaged [modified] Pin
akhanal28-Sep-09 9:28
akhanal28-Sep-09 9:28 
GeneralRe: Marshaling between managed and unmanaged [modified] Pin
Luc Pattyn28-Sep-09 9:43
sitebuilderLuc Pattyn28-Sep-09 9:43 
GeneralRe: Marshaling between managed and unmanaged [modified] Pin
akhanal28-Sep-09 10:06
akhanal28-Sep-09 10:06 
your code seems fine, it is what I would do given your description of the situation at hand.
except for the "I am using arg3 value over here" comment which doesn't fit the following line...


I have edited this in my previous message now.

To debug, I would set up a slightly larger experiment, that generates say some 10 to 100 numbers of known value (mostly different), and dump the results to see if anything throws and/or anything gets recognized; I tend to use hex notation for debugging, and logging (i.e. write to console or file, not rely on watch windows).

How do you write these data in hex form to the file. Using System.Diagnostics.Debug? I am new to this environment.

If that doesn't help, I would allocate a managed array, fill it with numbers 1...N and pass its pointer (using GCHandle) to the native side, then watch the array when it returns.

I did try this earlier but without using the GCHandle. This is what I did:

DllImport("SomeDLL.dll", EntryPoint = "func1")]
unsafe public static extern uint func1(UInt32 arg1, UInt32 arg2, ref Uint32[] arg3);

And I am calling this function in my code as:

{
........
UInt32 arg1;
UInt32 arg2;

//IntPtr arg3 = IntPtr.Zero; //made arg3 as Intptr
Uint32 [] managedArray = new Uint32[1]; //created a managed array

uint test;

...........
test = func1(arg1, arg2, ref managedArray);

............
/* I am using arg3 value over here*/

if(managedArray[0] == null)
{
....
}
}

It gives a similar kind of garbage value though.

I will try it with GCHandle, probably that will make it more accessible (or is it the only way to make it accessible?) to the unmanaged code.

Thanks

[ADDED]
BTW: when copying the data from an unmanaged to a managed object/array, you now have two objects; you should not forget to get rid of the unmanaged object somehow (i.e. if the native code did a malloc, you should call a function that performs a free).

There is another function that I have imported from the dll that frees the memory allocated by the unmanaged code:

public static extern uint freeMemory(IntPtr arg);
[/ADDED]

modified on Monday, September 28, 2009 4:36 PM

GeneralRe: Marshaling between managed and unmanaged Pin
Luc Pattyn28-Sep-09 10:45
sitebuilderLuc Pattyn28-Sep-09 10:45 
GeneralRe: Marshaling between managed and unmanaged Pin
akhanal28-Sep-09 11:03
akhanal28-Sep-09 11:03 
GeneralRe: Marshaling between managed and unmanaged Pin
akhanal28-Sep-09 12:01
akhanal28-Sep-09 12:01 
GeneralRe: Marshaling between managed and unmanaged Pin
Luc Pattyn28-Sep-09 12:18
sitebuilderLuc Pattyn28-Sep-09 12:18 
GeneralRe: Marshaling between managed and unmanaged Pin
akhanal29-Sep-09 4:04
akhanal29-Sep-09 4:04 
GeneralRe: Marshaling between managed and unmanaged Pin
Luc Pattyn29-Sep-09 4:17
sitebuilderLuc Pattyn29-Sep-09 4:17 
GeneralRe: Marshaling between managed and unmanaged [modified] Pin
akhanal29-Sep-09 5:02
akhanal29-Sep-09 5:02 
GeneralRe: Marshaling between managed and unmanaged Pin
Luc Pattyn29-Sep-09 5:36
sitebuilderLuc Pattyn29-Sep-09 5:36 
GeneralRe: Marshaling between managed and unmanaged Pin
akhanal29-Sep-09 5:32
akhanal29-Sep-09 5:32 
GeneralRe: Marshaling between managed and unmanaged Pin
Luc Pattyn29-Sep-09 5:37
sitebuilderLuc Pattyn29-Sep-09 5:37 
GeneralRe: Marshaling between managed and unmanaged [modified] Pin
akhanal29-Sep-09 6:01
akhanal29-Sep-09 6:01 
GeneralRe: Marshaling between managed and unmanaged Pin
Luc Pattyn29-Sep-09 6:15
sitebuilderLuc Pattyn29-Sep-09 6:15 
GeneralRe: Marshaling between managed and unmanaged Pin
akhanal29-Sep-09 6:27
akhanal29-Sep-09 6:27 
QuestionGetting the selected items from a radioButton group Pin
Twyce28-Sep-09 6:42
Twyce28-Sep-09 6:42 
AnswerRe: Getting the selected items from a radioButton group Pin
Michael Eber28-Sep-09 7:04
Michael Eber28-Sep-09 7:04 
QuestionCrystal Report at User's Computer Pin
Said Ali Jalali28-Sep-09 5:05
Said Ali Jalali28-Sep-09 5:05 
AnswerRe: Crystal Report at User's Computer Pin
Not Active28-Sep-09 5:12
mentorNot Active28-Sep-09 5:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.