Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / October 2005

Tip: Looking for answers? Try searching our database.

IntPtr marshal causes exception

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MLM450@hotmail.com - 20 Oct 2005 19:15 GMT
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);
Robert Jordan - 20 Oct 2005 19:19 GMT
> 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
Peter Liu - 25 Oct 2005 00:11 GMT
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);

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.