Click here to Skip to main content
15,909,827 members
Home / Discussions / C#
   

C#

 
GeneralRe: how to Add history to menu...? Pin
Kiran Satish20-Oct-04 12:30
Kiran Satish20-Oct-04 12:30 
GeneralListView Column Header Font Change Pin
Tony D. Abel19-Oct-04 13:44
Tony D. Abel19-Oct-04 13:44 
GeneralRe: ListView Column Header Font Change Pin
Nick Parker19-Oct-04 15:39
protectorNick Parker19-Oct-04 15:39 
General.net Variable Naming Conventions Pin
Jayman91119-Oct-04 13:18
Jayman91119-Oct-04 13:18 
GeneralRe: .net Variable Naming Conventions Pin
Steven Campbell19-Oct-04 13:22
Steven Campbell19-Oct-04 13:22 
GeneralRe: .net Variable Naming Conventions Pin
Jayman91119-Oct-04 13:28
Jayman91119-Oct-04 13:28 
GeneralAccessing form controls in code Pin
isittheweekendyet19-Oct-04 12:59
isittheweekendyet19-Oct-04 12:59 
GeneralRe: Accessing form controls in code Pin
Anonymous19-Oct-04 13:25
Anonymous19-Oct-04 13:25 
GeneralRe: Accessing form controls in code Pin
Nick Parker19-Oct-04 13:26
protectorNick Parker19-Oct-04 13:26 
GeneralRe: Accessing form controls in code Pin
isittheweekendyet19-Oct-04 14:34
isittheweekendyet19-Oct-04 14:34 
GeneralRe: Accessing form controls in code Pin
Nick Parker19-Oct-04 15:12
protectorNick Parker19-Oct-04 15:12 
GeneralRe: Accessing form controls in code Pin
isittheweekendyet20-Oct-04 0:19
isittheweekendyet20-Oct-04 0:19 
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 

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.