473,387 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Calling a dll using pinvoke from c# is much slower than calling it in C++

I have a propertary library dll (used to drive a device) that I call
from my C# code.
Calling the functions from C++ is really faster than calling them in C+
+. From C++ the call is almost instantaneous, from C# is about 1.5
seconds long.

For example, this command powers a device based on an interface switch
and returns a message in Ack_Msg that is len long. Besides returns an
error code.

The C++ function header is:
long __cdecl Grid_On_Off(BOOL *GridsOnOff, char Ack_Msg[], long len);

in C++ is called as:
void CCanCtrlDlg::OnGridOnOff(NMHDR* pNMHDR, LRESULT* pResult)
{
char cAcknowledge_Msg[300];
BOOL bGrids = m_Grids.GetPos();
long lError = C_Grid_On_Off(&bGrids, cAcknowledge_Msg, LG_MAX_STR);
}

I declare it in C# as
[DllImport("device_library", CharSet = CharSet.Ansi, CallingConvention
= CallingConvention.Cdecl)]
public static extern int Grid_On_Off(ref bool GridsOnOff,
[MarshalAs(UnmanagedType.LPStr)] StringBuilder Ack_Msg, int len);

I call it in C# from inside a button in the following way:
private void checkBoxGridOnOff_CheckedChanged(object sender, EventArgs
e)
{
StringBuilder ack_Msg = new StringBuilder(300);
bool tf = checkBoxOnOff.Checked;
int error = Grid_On_Off(ref tf, ack_Msg, 300)
}

What is the reason calling from C# is much slower? Can I do something
about it?
Thank you very much.

Jul 13 '07 #1
3 5202
Beorne,

Not really. P/Invoke has to load the dll (a call to LoadLibrary), then
get the procedure address (GetProcAddress), and then marshal all the
parameters from managed memory to unmanaged memory, construct the stack, and
then make the call.

It then has take the results off the stack and marshal them back to
managed code.

Needless to say, there is always going to be some overhead when making
calls through P/Invoke.

The overhead is not there in C++ because C++ doesn't have to call
LoadLibrary, doesn't have to marshal parameters, and doesn't have to set up
the stack for calling conventions and the like (not in the way the runtime
has to do it, at least).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Beorne" <ma*******@gmail.comwrote in message
news:11**********************@n2g2000hse.googlegro ups.com...
>I have a propertary library dll (used to drive a device) that I call
from my C# code.
Calling the functions from C++ is really faster than calling them in C+
+. From C++ the call is almost instantaneous, from C# is about 1.5
seconds long.

For example, this command powers a device based on an interface switch
and returns a message in Ack_Msg that is len long. Besides returns an
error code.

The C++ function header is:
long __cdecl Grid_On_Off(BOOL *GridsOnOff, char Ack_Msg[], long len);

in C++ is called as:
void CCanCtrlDlg::OnGridOnOff(NMHDR* pNMHDR, LRESULT* pResult)
{
char cAcknowledge_Msg[300];
BOOL bGrids = m_Grids.GetPos();
long lError = C_Grid_On_Off(&bGrids, cAcknowledge_Msg, LG_MAX_STR);
}

I declare it in C# as
[DllImport("device_library", CharSet = CharSet.Ansi, CallingConvention
= CallingConvention.Cdecl)]
public static extern int Grid_On_Off(ref bool GridsOnOff,
[MarshalAs(UnmanagedType.LPStr)] StringBuilder Ack_Msg, int len);

I call it in C# from inside a button in the following way:
private void checkBoxGridOnOff_CheckedChanged(object sender, EventArgs
e)
{
StringBuilder ack_Msg = new StringBuilder(300);
bool tf = checkBoxOnOff.Checked;
int error = Grid_On_Off(ref tf, ack_Msg, 300)
}

What is the reason calling from C# is much slower? Can I do something
about it?
Thank you very much.

Jul 13 '07 #2
"Beorne" <ma*******@gmail.comwrote in message
news:11**********************@n2g2000hse.googlegro ups.com...
>I have a propertary library dll (used to drive a device) that I call
from my C# code.
Calling the functions from C++ is really faster than calling them in C+
+. From C++ the call is almost instantaneous, from C# is about 1.5
seconds long.
How did you measure this? I can't believe this takes 1.5 seconds unless you
are loading the library (device_library) over a network connection, or you
are running this on an (over)loaded system and too slow system.
Note that the initial call is somewhat slower than succeeding calls, this
because the library has to be loaded first, but this can't hardly take 1.5
seconds. Succeeding calls only take a hit because of the char[] marshaling,
but again this is a matter of fractions_of µseconds.
Willy.

Jul 13 '07 #3
You are right, the slow response was due to a timer set too tight,
pinvoke marshalling is in general fast enough.

Thanks!
On 13 Lug, 20:40, "Willy Denoyette [MVP]" <willy.denoye...@telenet.be>
wrote:
"Beorne" <matteo...@gmail.comwrote in message

news:11**********************@n2g2000hse.googlegro ups.com...
I have a propertary library dll (used to drive a device) that I call
from my C# code.
Calling the functions from C++ is really faster than calling them in C+
+. From C++ the call is almost instantaneous, from C# is about 1.5
seconds long.

How did you measure this? I can't believe this takes 1.5 seconds unless you
are loading the library (device_library) over a network connection, or you
are running this on an (over)loaded system and too slow system.
Note that the initial call is somewhat slower than succeeding calls, this
because the library has to be loaded first, but this can't hardly take 1.5
seconds. Succeeding calls only take a hit because of the char[] marshaling,
but again this is a matter of fractions_of µseconds.

Willy.

Jul 18 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Mark | last post by:
I am using gnu g++ version 3.3.2, trying a simple test to read in and then write out a large (100,000 line) text file ########################################## CSTDIO VERSION TO READ/WRITE...
15
by: Ingmar | last post by:
Simple comparison tests we have performed show that System.Math functions in C# are much slower than corresponding functions in C++. Extreme examples are System.Math.Exp() and System.Math.Tan(),...
4
by: dhnriverside | last post by:
Hi peeps I'm having some problems with my Session State sticking (it keeps resetting itself) - I haven't looked into it yet, but I was wondering about using SQL Server as an out of process state...
13
by: Ron L | last post by:
I am working on an application that is a front-end for a SQL database. While it is not an immediate requirement, the application will probably be required to be able to connect via the internet at...
12
by: jimocz | last post by:
Did I do something wrong? I cross posted this on the dotnet development group -- sorry if it is a double posting but we are seriously considering going to c# and this could be a show stopper. ...
6
by: Wayne | last post by:
Has anyone noticed that Access databases (2003) are much slower to load under Vista or is it just me? I have a dual boot machine (XP and Vista). Applications generally are a bit slower to load...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.