Click here to Skip to main content
15,905,233 members
Home / Discussions / C#
   

C#

 
GeneralRe: Best practices with password storage/use Pin
Nick Parker19-Oct-04 12:52
protectorNick Parker19-Oct-04 12:52 
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 
First - a few words about your unmanaged signatures.

You should almost never return void. For your "free" functions, this can be okay but sometimes you might gets errors back that would be good to know about.

Mostly, never return pointers. Return error codes (like HRESULTs) and declare your previous return values as [out] parameters. If you did that, your unmanaged to managed mapping would be easy and you wouldn't have to worry about marshaling the struct yourself:
// unmanaged
__declspec(dllexport) HRESULT GetHostInfoFromDB(int iHostID, P_HostInfo hostInfo);
 
// managed
[DllImport(...)]
extern static int GetHostInfoFromDB(int iHostID, out HostInfo hostInfo);
I would also warn that if you want to support both 32- and 64-bit architectures, your native int is equivalent to an IntPtr (both platform-dependent bit widths).

Moving on, though...

You need to pin your structure in memory using the GCHandle class (see the documentation in the .NET Framework SDK). The GC can move this around so that when you pass the address back to FreeHostInfoStruct. You can also use Marshal.DestroyStructure, which you typically should use when you use Marshal.PtrToStructure. In this case, however - since your freeing memory in unmanaged code - pinning the object should solve the problem.

This posting is provided "AS IS" with no warranties, and confers no rights.

Software Design Engineer
Developer Division Sustained Engineering
Microsoft

[My Articles] [My Blog]
GeneralRe: Question about Interop with a DLL written in C++ Pin
kmansari20-Oct-04 10:02
kmansari20-Oct-04 10:02 
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 

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.