Click here to Skip to main content
15,509,806 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:

I have a dll supplied by our vendor.

Now if I declare the following function like this:
Imports System.Runtime.InteropServices

Public Class clsLasalComm

Private Declare Function Online Lib "Lasal32.dll" (ByVal szPort As String, ByVal uBaudrate As Byte, ByVal uPcStation As Byte, ByVal uSpsStation As Byte, ByVal uAutoInit As Byte) As Integer

end class

It just works fine.

But if I declare it like this:
Imports System.Runtime.InteropServices

Public Class clsLasalComm
   <DllImport("Lasal32.DLL", EntryPoint:="Online", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
   Private Shared Function Online(ByVal szComm As String, ByVal uBaudRate As Byte, ByVal uPcStation As Byte, ByVal uSpsStation As Byte, ByVal uAutoInit As Byte) As Integer
   End Function
end class

It no longer gives the correct outcome.

Can anyone tell me why, or explain the differences in declaration.
I am fairly new to and have understood the latter way is better.

Richard MacCutchan 30-Mar-15 5:45am    
It no longer gives the correct outcome
Do you mean at compile time or run time? And what exactly is the outcome? Have you checked with your vendor that you are selecting the correct CallingConvention option?
Member 11109125 30-Mar-15 8:42am    
It is a function to set up an online connection with a PLC. With the first declaration it works fine in runtime. With the second declaration it no longer works at runtime.
I am not sure about the "callingconvention". What is it exactly? What difference could it make?
Richard MacCutchan 30-Mar-15 9:19am    
I suggest you contact the people who own the DLL and ask them for advice.
Dave Kreskowiak 30-Mar-15 10:19am    
It affects how parameters are pushed onto the stack before the call to the function is made. Get this wrong and the function gets the wrong parameters, obviously affecting the outcome of the function call.

Again, the only people who can tell you what the correct calling convention should be is the vendor of the .DLL you're using.

1 solution


I tried all 5 options for the "CallingConvention", neither of them seem to work. So I am not sure if that is where the problem lies.

The supplier has some documentation but that is based on C/C++.
Here's the declaration from the Lasal32.h header file:
//--- Online schalten --------------------------------------------------------------------
//->      szPort: "LPT1" ... CAN-Adapter an LPT1
//                "LPT2" ... CAN-Adapter an LPT2
//                "COM1" ... RS232 ueber COM1
//                "COM2" ... RS232 ueber COM2
//                "COM3" ... RS232 ueber COM3
//                "COM4" ... RS232 ueber COM4
//                "" ... TCP/IP mit Angabe der IP-Adresse
//     die restlichen Parameter sind nur bei CAN-Betrieb notwendig (sonst 0 übergeben)!
//->   uBaudrate: 0 = 615 KBaud
//                1 = 500 KBaud
//                2 = 250 KBaud
//                3 = 125 KBaud
//                4 = 100 KBaud
//                5 =  50 KBaud
//                6 =  20 KBaud
//->  uPcStation: 0-30 (eigen Stationsnummer)
//-> uSpsStation: 0-30 (auf der SPS eingestellte CAN-Stationsnummer)
//->   uAutoInit: 1 = automatische Neuinitialisierung bei Störung (z.B: durch ein Dongle)
// Hinweis: Zwischen 2 Aufrufen von Online() muß jedenfalls die Funktion Offline()
//            aufgerufen werden!

#define LSL_ONLINE_CAN_BAUD615K		0
#define LSL_ONLINE_CAN_BAUD500K		1
#define LSL_ONLINE_CAN_BAUD250K		2
#define LSL_ONLINE_CAN_BAUD125K		3
#define LSL_ONLINE_CAN_BAUD100K		4

LSL_BOOL LASAL32_EXPORTS Online(const char* szComm, uint8_t uBaudRate, uint8_t uPcStation, uint8_t uSpsStation, uint8_t uAutoInit);

In a C# example from the supplier the function is declared as follows:
/// <summary>
/// open connection-port to plc
/// </summary>
/// <param name="ocbNum"></param>
/// <param name="szComm"></param>
/// <param name="uBaudRate"></param>
/// <param name="uPcStation"></param>
/// <param name="uSpsStation"></param>
/// <param name="uAutoInit"></param>
/// <returns></returns>
public static extern bool Online(string szComm, byte uBaudRate, byte uPcStation, byte uSpsStation, byte uAutoInit);

Can anything be told from this?
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900