all groups > dotnet interop > august 2007 >
You're in the

dotnet interop

group:

C# - C++ DLL performance woes


C# - C++ DLL performance woes Charles Jenkins
8/28/2007 6:35:08 AM
dotnet interop:
I have a C# application that calls a lower-level C++ dll to do serial
communication. (Using C++ for serial I/O is not necessary, but I was asked
to do this.)

Calls from C# to C++ functions generally look like this:

[DllImport( DLL_NAME, EntryPoint = "SetMultiplier" )]
private static extern WFGP_MSG DllSetMultiplier(
Int16 channel, UInt16 mult
);

All the calls made during a communication session use types that require no
translation in order to cross the boundary between managed and unmanaged
code. The P/Invoke calls should be just as fast as P/Invoke can be.

Everything works just fine. However, the application tends to "freeze" for
a few seconds after every few minutes of use. No data gets lost, so the
freeze is more of an annoyance than anything. However, I feel sure that it
is caused by something I am not doing correctly in the way I use unmanaged
code. My first instinct would be do assume that regular, long pauses are
caused by creating many, many objects that almost immediately go out of
scope, leaving a huge amount of work for the garbage collector. However, I
do not believe my code is creating tons of objects. Could a P/Invoke call
create a lot of temporary objects behind the scenes?

Does anyone know how you go about determining what causes regular, long
Re: C# - C++ DLL performance woes Charles Jenkins
8/28/2007 1:39:10 PM
Mattias Sjögren <mattias.dont.want.spam@mvps.org> wrote in
news:uNx6jca6HHA.4712@TK2MSFTNGP04.phx.gbl:

[quoted text, click to view]

Re: C# - C++ DLL performance woes Mattias Sjögren
8/28/2007 9:10:02 PM

[quoted text, click to view]

No, at least no managed objects. Not when using such simple parameter
types.


[quoted text, click to view]

I'd start by using a profiler.


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Re: C# - C++ DLL performance woes Marcus Heege
9/4/2007 5:20:39 PM
One optimization option that you should think of is
[SuppressUnmanagedCodeSecurity]

Marcus Heege

[quoted text, click to view]
AddThis Social Bookmark Button