473,756 Members | 3,566 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(BOO L *GridsOnOff, char Ack_Msg[], long len);

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

I declare it in C# as
[DllImport("devi ce_library", CharSet = CharSet.Ansi, CallingConventi on
= CallingConventi on.Cdecl)]
public static extern int Grid_On_Off(ref bool GridsOnOff,
[MarshalAs(Unman agedType.LPStr)] StringBuilder Ack_Msg, int len);

I call it in C# from inside a button in the following way:
private void checkBoxGridOnO ff_CheckedChang ed(object sender, EventArgs
e)
{
StringBuilder ack_Msg = new StringBuilder(3 00);
bool tf = checkBoxOnOff.C hecked;
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 5267
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.co m

"Beorne" <ma*******@gmai l.comwrote in message
news:11******** **************@ n2g2000hse.goog legroups.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(BOO L *GridsOnOff, char Ack_Msg[], long len);

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

I declare it in C# as
[DllImport("devi ce_library", CharSet = CharSet.Ansi, CallingConventi on
= CallingConventi on.Cdecl)]
public static extern int Grid_On_Off(ref bool GridsOnOff,
[MarshalAs(Unman agedType.LPStr)] StringBuilder Ack_Msg, int len);

I call it in C# from inside a button in the following way:
private void checkBoxGridOnO ff_CheckedChang ed(object sender, EventArgs
e)
{
StringBuilder ack_Msg = new StringBuilder(3 00);
bool tf = checkBoxOnOff.C hecked;
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*******@gmai l.comwrote in message
news:11******** **************@ n2g2000hse.goog legroups.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...@gmai l.comwrote in message

news:11******** **************@ n2g2000hse.goog legroups.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
5539
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 TEXT FILE: #include <cstdlib> #include <cstdio> using namespace std;
15
12247
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(), which both seem to average 50 times slower than their C++ counterparts. (Tests are done using optimized Release configurations outside the Visual Studio environment.) Our explanation for this large discrepancy is that C# is an interpreter...
4
2099
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 manager? I've heard it's slower - and I was just wondering... how much slower? (this is for an Intranet app) Cheers
13
1923
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 a later date, so we are implementing the data connections via remoting. The remoting is implemented via the HTTP channel, with the binary formatter. We have noticed, however, that the remoting calls seemed to be much slower that the OLEDB calls,...
12
1858
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. I ran the following C# program and it ran in 9 seconds (give or take 1 sec) using System; using System.Collections.Generic; using System.Text;
6
2041
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 under Vista, Access included, and an Access DB of say 5mb size takes several seconds to load out of memory under Vista and is instantaneous under XP. Speed is definitely not one of the new OS's strong points.
0
9275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9872
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9843
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9713
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.