Click here to Skip to main content
16,012,468 members
Home / Discussions / C#
   

C#

 
GeneralRe: Best practices with password storage/use Pin
Steven Campbell19-Oct-04 13:20
Steven Campbell19-Oct-04 13:20 
Generalinsert picture in listbox Pin
hudhud19-Oct-04 10:50
hudhud19-Oct-04 10:50 
GeneralRe: insert picture in listbox Pin
Nick Parker19-Oct-04 15:59
protectorNick Parker19-Oct-04 15:59 
GeneralCompare lines in two large txt files Pin
sverre.andersen19-Oct-04 10:39
sverre.andersen19-Oct-04 10:39 
GeneralRe: Compare lines in two large txt files Pin
Nick Parker19-Oct-04 13:16
protectorNick Parker19-Oct-04 13:16 
GeneralQuestion about Interop with a DLL written in C++ Pin
kmansari19-Oct-04 10:23
kmansari19-Oct-04 10:23 
GeneralRe: Question about Interop with a DLL written in C++ Pin
Heath Stewart19-Oct-04 16:02
protectorHeath Stewart19-Oct-04 16:02 
GeneralRe: Question about Interop with a DLL written in C++ Pin
kmansari20-Oct-04 10:02
kmansari20-Oct-04 10:02 
Heath,
Thanks a lot for your post. I completlely agree with everything you said about my unmanaged signatures. Unfortunately, the DLL that I am trying to interface with is also being used by other groups in my company. Making it return HRESULT (and other accompanying changes) would be a substantial effort, since it is not just one API we are talking about here.

About your suggestion to pin down the structure before I can pass its address to the free function: Is it really required since the memory was allocated in the unmanaged world? If my understanding is right (and most likely it is not), the GC doesn't care about unmanaged memory.

So let's go through the example once again:

1. From my managed code, I call GetHostInfoFromDB (this is exported by my unmanaged HostInfo.DLL)
2. GetHostInfoFromDB allocates unmanaged memory, initializes it with the data from the DB, and returns me a pointer to the unmanaged block of memory.
3. In my managed world, what I get is an IntPtr. So I use PtrToStruct to be able to see my data.
4. After processing the data, I have to call the unmanaged code again - this time, I am requesting it to free up the memory that it allocated during the first call
5. It is now that I pass the IntPtr that I got back from GetHostInfoFromDB. However, it turns out that the value of IntPtr received by the unmanaged world is different that what was passed from the managed code.

Now, going back to your suggestion about pinning down the memory: I wasn't sure which memory you wanted me to pin down, since we are not allocating any memory in the managed world. However, I took your suggestion and pinned down the IntPtr that I get back from GetHostInfoFromDB.

IntPtr iPtr = GetHostInfoFromDb (0); // Get the pointer to host info
GCHandle gcHandle = GCHandle.Alloc (iPtr, GCHandleType.Pinned); // Pin down the structure
IntPtr hostInfoStruct = gcHandle.AddrOfPinnedObject(); // Get the addr. of the pinned struct

/* --- Call PtrToStruct here, and consume the host information here *---/

FreeHostInfoStruct (hostInfoStruct); // free up the memory
gcHandle.Free (); // finally, free up the GC handle

I still see the same problem with those changes.

Next, I tried using
FreeHostInfoStruct ((IntPtr)gcHandle); // free up the memory
instead of
FreeHostInfoStruct (hostInfoStruct); // free up the memory

But that didn't solve the problem either. Any more ideas?

Thanks,
-Kamran

GeneralRe: Question about Interop with a DLL written in C++ Pin
kmansari20-Oct-04 10:24
kmansari20-Oct-04 10:24 
GeneralRe: Question about Interop with a DLL written in C++ Pin
Heath Stewart20-Oct-04 13:37
protectorHeath Stewart20-Oct-04 13:37 
Generalmessaging in C# Pin
ppp00119-Oct-04 10:01
ppp00119-Oct-04 10:01 
GeneralRe: messaging in C# Pin
Heath Stewart19-Oct-04 15:46
protectorHeath Stewart19-Oct-04 15:46 
GeneralThreading -- Performance Pin
petst19-Oct-04 9:42
petst19-Oct-04 9:42 
GeneralRe: Threading -- Performance Pin
Salil Khedkar19-Oct-04 20:47
Salil Khedkar19-Oct-04 20:47 
GeneralRe: Threading -- Performance Pin
petst20-Oct-04 4:28
petst20-Oct-04 4:28 
GeneralSpecifying number of digits after decimal in a fixed-point formatted string Pin
DTWC_Lawrence19-Oct-04 9:40
DTWC_Lawrence19-Oct-04 9:40 
GeneralRe: Specifying number of digits after decimal in a fixed-point formatted string Pin
Alex A. Miller19-Oct-04 9:57
Alex A. Miller19-Oct-04 9:57 
GeneralRe: Specifying number of digits after decimal in a fixed-point formatted string Pin
DTWC_Lawrence19-Oct-04 10:02
DTWC_Lawrence19-Oct-04 10:02 
GeneralRe: Specifying number of digits after decimal in a fixed-point formatted string Pin
Salil Khedkar19-Oct-04 21:17
Salil Khedkar19-Oct-04 21:17 
GeneralShrinking Toolbar images Pin
LannieK19-Oct-04 9:24
LannieK19-Oct-04 9:24 
GeneralRe: Shrinking Toolbar images Pin
Heath Stewart19-Oct-04 11:21
protectorHeath Stewart19-Oct-04 11:21 
GeneralRe: Shrinking Toolbar images Pin
LannieK20-Oct-04 2:23
LannieK20-Oct-04 2:23 
GeneralRe: Shrinking Toolbar images Pin
Heath Stewart20-Oct-04 5:20
protectorHeath Stewart20-Oct-04 5:20 
GeneralRe: Shrinking Toolbar images Pin
LannieK20-Oct-04 10:03
LannieK20-Oct-04 10:03 
General32 bit integer from sbyte array Pin
BrcKcc19-Oct-04 9:03
BrcKcc19-Oct-04 9:03 

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.