Click here to Skip to main content
15,900,461 members
Home / Discussions / C#
   

C#

 
GeneralBest practices with password storage/use Pin
econner19-Oct-04 12:01
econner19-Oct-04 12:01 
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 
I have a DLL that was written using C++. I am required to write a C# program that calls into the DLL and gets some information out of it. The DLL entry points that I have to call are declared as:

__declspec(dllexport) HostInfo* GetHostInfoFromDb (int iHostId);
__declspec(dllexport) void FreeHostInfoStruct (void* pHostInfo);

struct {
char* pHostName;
int iId;
char* pOwner;
...
...
} HostInfo, *P_HostInfo;


HostInfo is a structure containing a bunch of flat fields (i.e, all basic data types except for a couple of char*'s ). When GetHostInfoFromDb is called, it allocates a HostInfo structure internally, populates it with the information from a database, and retuns me the pointer to it.

The way I did this in C# is as follows:

[DllImport @"hostinfo.dll",EntryPoint="GetHostInfoFromDb")]
public static extern IntPtr GetHostInfoFromDb(int iHostId);

[DllImport @"hostinfo.dll",EntryPoint="FreeHostInfoStruct")]
public static extern void FreeHostInfoStruct(IntPtr pHostInfo);

[StructLayout(LayoutKind.Sequential,Size=116,CharSet=CharSet.Ansi )]
public struct ManagedHostInfo
{
string szHostName;
int iId;
string szOwner;
.....
.....
}

I could successfully get my DLL to return the information I was looking for by calling GetHostInfoFromDb. I marshal the returned IntPtr using Marshal.PtrToStructure. However, when I try to call FreeHostInfoStruct, the DLL doesn't seem to free up the memory it had allocated. After a few hundred calls, the memory kept growing and started impacting the system performance.

I verified this by first printing out the address of the HostInfo struct that the DLL allocated during a call to GetHostInfoFromDb. Next, I print out the address of pHostInfo during my call to FreeHostInfoStruct. The two addresses are different. What was initially allocated is not being freed up during FreeHostInfoStruct.

C# code C++ Code
---------------------------------------------------------------------------------------
1. IntPtr iPtr = GetHostInfoFromDb (0); ---> P_HostInfo pInfo = new HostInfo ();

// Populate pInfo fields
// using info from database
return pInfo; //Say, pInfo = 0x46F53A44

2. ManagedHostInfo hostInfo =
Marshal.PtrToStructure (iPtr);
// If you look at iPtr, it is the
// same as pInfo (=0x46F53A44)

3. FreeHostInfoStruct (iPtr); ---> void FreeHostInfoStruct (void* pHostInfo)
{
// if you check out pHostInfo here,
// it's different that what was
// originally passed by the caller.
// So in our example, this won't
// be 0x46F53A44, but something else!!
}

So what am I doing wring here? Please help!!
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 
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 

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.