B
Beorne
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.
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.