I am writing a C++ program that is using a C# control. It calls a
function that requires a handle to data in memory. If I set up the C#
function to take this parameter as a uint, it works fine. I would
rather set up the parameter as an IntPtr. But when I do that, I get an
exception when the C++ program calls the function.
Any idea why the exception occurs?
Thanks!
C++ Code:
HANDLE MyData = GlobalAlloc(GMEM_MOVEABLE,File.GetLength());
LPVOID Mem = GlobalLock(MyData);
[... populate data ...]
GlobalUnlock(MyData);
MyControl->MyFunction((long)Data);
GlobalFree(MyData);
C# Interface Function:
bool MyFunction(IntPtr hData);
> I am writing a C++ program that is using a C# control. It calls a
> function that requires a handle to data in memory. If I set up the C#
> function to take this parameter as a uint, it works fine. I would
> rather set up the parameter as an IntPtr. But when I do that, I get an
> exception when the C++ program calls the function.
What kind of exception? What is your C# method doing with the IntPtr?
Rob
MLM450@hotmail.com - 21 Oct 2005 13:01 GMT
I am not sure what the exception is because I am having some difficulty
trapping it. I will try again.
What my C# method does with the IntPtr seems to be irrelevant. The
exception is occurring somewhere after the call in C++ and before my C#
function gets it.
> > I am writing a C++ program that is using a C# control. It calls a
> > function that requires a handle to data in memory. If I set up the C#
[quoted text clipped - 5 lines]
>
> Rob
MLM450@hotmail.com - 21 Oct 2005 13:04 GMT
I am not sure what the exception is because I am having some difficulty
trapping it. I will try again.
What my C# method does with the IntPtr seems to be irrelevant. The
exception is occurring somewhere after the call in C++ and before my C#
function gets it.
> > I am writing a C++ program that is using a C# control. It calls a
> > function that requires a handle to data in memory. If I set up the C#
[quoted text clipped - 5 lines]
>
> Rob
MLM450@hotmail.com - 21 Oct 2005 13:07 GMT
The general message I get in the exception is:
"An unhandled exception of type
'System.Runtime.InteropServices.SEHException' occurred in HOST.exe
Additional information: External component has thrown an exception"
> > I am writing a C++ program that is using a C# control. It calls a
> > function that requires a handle to data in memory. If I set up the C#
[quoted text clipped - 5 lines]
>
> Rob
In C#, the memory is managed by GC, so the memory address is not fixed.
>I am writing a C++ program that is using a C# control. It calls a
> function that requires a handle to data in memory. If I set up the C#
[quoted text clipped - 16 lines]
> C# Interface Function:
> bool MyFunction(IntPtr hData);
MLM450@hotmail.com - 31 Oct 2005 21:20 GMT
Thanks for the response, but I don't see how that is relevant here.
Perhaps you can elaborate? I am not passing around pointers or anything
like that. I am passing a handle to memory. Also, I don't get a chance
to do anything with the value passed to me. The exception occurs
somewhere between my C++ program and my C# program.
> In C#, the memory is managed by GC, so the memory address is not fixed.
>
[quoted text clipped - 18 lines]
> > C# Interface Function:
> > bool MyFunction(IntPtr hData);