|
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!!
|
|
|
|
|
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 HRESULT s) 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:
__declspec(dllexport) HRESULT GetHostInfoFromDB(int iHostID, P_HostInfo hostInfo);
[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]
|
|
|
|
|
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
|
|
|
|
|
Heath,
What finally did solve the problem was undoing a stupid copy-and-paste mistake which had just got lost in the jungle of declarations... and the mistake? My managed signature declared the FreeHostInfoStruct function as returning a structure by value (DUH!!!)
I modified it to return a void (which is how the unmanaged declaration was), everything started to work just fine. I guess declaring it the wrong way might have caused the .NET CLR to construct a screwed up call stack.
I still have the GCHandle question for you though. Am I really required to pin down memory that was allocated in the unmanaged world?
Thanks a lot for your help.
-Kamran
|
|
|
|
|
kmansari wrote:
I guess declaring it the wrong way might have caused the .NET CLR to construct a screwed up call stack.
Keep in mind that the term unmanaged means that the CLR does not managed the memory. When you call into unmanaged code, the CLR has no way of tracking memory. Just FYI.
kmansari wrote:
I still have the GCHandle question for you though. Am I really required to pin down memory that was allocated in the unmanaged world?
No, but when alloc'ing it in the managed world and passing it to unmanaged code you do. Depending on what calls you make, though - like Marshal.PtrToStructure - you may need to pin the object because once you marshal the struct the GC tracks it and may move it. Chances are that won't happen - especially with value types which are declared on the stack - but it just something to keep in mind for future development.
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]
|
|
|
|
|
Hi all,
What is the best methodology for messaging (IPC) betweeen two applications
on the same PC by programming in C# ?
thanks
Patrick
|
|
|
|
|
.NET Remoting[^]
This describes a contract between two or more AppDomains with which to communicate. Think of it as a more advanced version of XML Web Services (though on the same machine - or even remote machines - using a BinaryFormatter with a TcpChannel is much faster to serialize and send through the wire - but it's an extensible system).
In .NET 2.0, a new IpcChannel will be supported for actual IPC calls (like for pipes, which you'd currently have to P/Invoke to use).
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]
|
|
|
|
|
Hi all,
An interresting problem in the context of interprocess communication. Consider following code:
<br />
public void StartSepThread()<br />
{<br />
ThreadStart tsThread = new ThreadStart(RunServerSepThread);<br />
TestThread = new Thread(tsThread);<br />
TestThread.Name = "IPC.Test";<br />
TestThread.Start();<br />
}<br />
public void RunServerSepThread()<br />
{<br />
while(true)<br />
{<br />
(lot of code here...)<br />
}<br />
}<br />
The code above proves to be 50% less performant than following code:
<br />
public void RunServerSameThread()<br />
{<br />
while(true)<br />
{<br />
(exact same code here...)<br />
}<br />
}<br />
When running the server in the same thread, we are able to move 163 Mbit/sec of data between two running processes. However, when running the same code as a seperate thread, performance goes down by almost 35% to a level of 111 Mbit/sec.
Anybody any idea what this might be???
Thanks
|
|
|
|
|
Ummm... Just in case you have misssed this, try Thread.Sleep(0); immediately after you do TestThread.Start(); method. Do post the resultant performance.
MSDN documentation on System.Threading.Thread overview says "On a uniprocessor machine, the (child) thread does not get any processor time until the main thread yields."
|
|
|
|
|
Well... the performance gain is nothing.
I didn't mentio it in my code posted, but inside the While loop there is a blocking call. This is exactly the same code in the single thread and new thread code. So that doesn't make any difference.
I believe the CPU would go 100% if no blocking call was inside the loop and Thread.Sleep(0) was missing...
Thanks
Peter
|
|
|
|
|
Hi,
Does anyone know of a way to specify the number of decimal places in a fixed-point formatted string in C#?
This is how I was doing it in VC++(6.0):
CString thisString;
double val = 12.3456789;
thisString.Format(_T("Format to 4 decimal places %.4f"), val);
Much thanks for any help!
B
|
|
|
|
|
double newW = 12.345678;
string pw = string.Format("{0:N3}", newW);
//pw is now 12.345
|
|
|
|
|
As always it is very humbling to have the forest pointed out!
Thanks for the quick reply.
B
|
|
|
|
|
Try this
<br />
double MyFloat = 123.456;<br />
string MyString;<br />
MyString = String.Format("{0:00.00}", MyFloat);<br />
Console.WriteLine(MyString);<br />
or even better
<br />
double MyFloat = 123.456;<br />
Console.WriteLine(String.Format("{0:00.00}", MyFloat));<br />
Salil Khedkar [^]
|
|
|
|
|
I am making my first attempt at using the ToolBar. I have defined an ImageList, and put a 16x16 pixel in it, and associated the first button in my ToolBar with index 0. The problem is, the image that is already only 16x16 shows up on the ToolBar button as shrunken to an even smaller size. AutoSize is set to true for the ToolBar, and the ImageList shows the image size as 16x16.
Am I missing an important setting? What could be causing the incredible shrinking image? And on a tangent, why doesn't the Transparent property of ImageList work?
|
|
|
|
|
Even though the toolbox button size is automatic, what does it report in the PropertyGrid for the ToolBar.ButtonSize ? This should be something larger than 16x16 since a margin is required to draw the buttons both in 3D and flat (for when you hover) mode.
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]
|
|
|
|
|
The ButtonSize is shown as 24, 24. That should be plenty for a 16x16 image. The button is displayed with a gap of several pixels between the shading on the edges of the button and the shrunken image.
|
|
|
|
|
Yeah, that definitely sounds right. Did you set the ImageList.ImageSize property before or after you added the images? It's important to have all properties set before you add images, including the ImageSize and TransparentColor . If you didn't set these first, you'll need to remove your ImageList (i.e., delete it completely) and recreate it with the proper property values, then add your images.
If you did perform those operations in that order, than frankly I'm at a loss. You could try to unassign the ImageList from the control, close the designer (perhaps close VS.NET entirely), then re-open the control and reassign the ImageList . Sometimes - but rarely in my experience - the designer state gets a little "off" and needs a kick in the pants. I doubt this is the problem though; it's rare and is often caused by massive number of changes to the designer, especially when working with custom controls.
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]
|
|
|
|
|
That solved the problem! I made a new ImageList, with the right properties set, added the exact same image to it, then associated the image list with the toolbar. Now the image is behaving properly, including the transparency. Thanks!
|
|
|
|
|
I have an sbyte array with a length of 4. What these four bytes represent is a single 32-bit number.
I don't know how to convert the 4 sbyte elements into a single 32-bit number in C#. I could do this easily in C or C++ by casting a pointer to the first element as a pointer to Long and then dereferencing the pointer to get at the long.
How do I do this in C#?
BRCKCC
|
|
|
|
|
Use BitConverter.ToInt32
The bitconverter class has many handy conversion routines.
This should help.
Dies
|
|
|
|
|
Use the shift operator >> to move the individual values to the right place, then add them. If you're not familiar with it, read this page:
http://www.c-sharpcorner.com/Language/BitWiserOpsInCSCH001.asp[^]
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
Hi all,
I want to use MSMQ in C# to perform messaging sending between two applications and have a few questions as below,
1. will MSMQ in c# consume much resources(memory and CPU) while it runs ?
2. Is MSMQ exist on Window 98 and Window Me if .Net on those platform is installed ?
3. can the MSMQ messaging by C# be programmed between two window services, or between on window service and one exe app ?
Thanks
Patrick
|
|
|
|
|
You msut perform performance benchmarks and determine if the results are adequate for your needs. Most often managed code requires more memory but requires about the same number of CPU cycles as unmanaged code - though not always (this depends on a number of factors, especially demanding CAS permissions that cause a stack walk).
If you look at the bottom of all the class documentation, the system requirements are specified.
Since the .NET BCL defines the MSMQ client code, it does not require MSMQ to be installed (which is supported on Win2K and newer). The "Platforms" section under "Requirements" states that Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family is supported.
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]
|
|
|
|
|
Hi,
I was reading the MCE SDK 2005, there is an example of MediaState, I’ve compiled it to test it on my MCE. I had to add the needed register component that i found on the SDK help file.
When i run the app, it does not get any information from the MediaCenter. What could be wrong? I think the COM object is not working properly because i cant read any information of MCE. Does anyone have a clue?
Greetings.
|
|
|
|
|