Visual Basic development

Post questions and issues with Concept2 PM3 SDK
Post Reply
chezmap
Paddler
Posts: 1
Joined: March 31st, 2006, 1:10 pm

Visual Basic development

Post by chezmap » March 31st, 2006, 1:32 pm

Hi,
I'd like to have a go at writing an application to interface with the PM3 but the only language I'm particularly familiar with is VD6. Can you tell me if the current development of the SDK is compatible with VB?

If you had a demo of the basic setup that would be superb!

Many thanks,
Mark.

haboustak
500m Poster
Posts: 77
Joined: March 17th, 2006, 3:02 pm
Location: Cincinnati

Post by haboustak » March 31st, 2006, 3:13 pm

Mark,

This post from the old forum: http://www.c2forum.com/viewtopic.php?t=87 was from a user who was trying to get the DLLs to work from inside VB6.

The SDK is NOT currently compatible with VB6, because it's defined in the DLL as using a c-style calling convention rather than the VB compatible stdcall.

There are some good links in that other post that will get you started. I'm not sure how much of the information that was linked to is still available, but basically what's necessary is an additional wrapper layer written in C/C++ that converts the cdecl DLLs into a stdcall version.

If you're willing to move to VB.NET or C# then you can import the functions in the DLLs even though they aren't stdcall. This is done by specifiying the calling convention in the .NET import statement. Let me know if you choose that route and I can post in the necessary import lines to get you started.

Mike

(Here's another good link on the .NET stuff: http://www.c2forum.com/viewtopic.php?t=88)

User avatar
Chris Brett
500m Poster
Posts: 61
Joined: May 25th, 2006, 10:07 am

Post by Chris Brett » May 25th, 2006, 10:46 am

An active X control is currently in development which will make coding PM3 applications in VB 6.0 and VBA fairly straightforward. In the hopefully short interim period however....

That link is very useful and the Chris Rey Code is a good starting point. Essentially you need to write/modify/copy a wrapper dll as per the link. The wrapper defines a stdcall function version
e.g

Code: Select all

 
W_tkcmdsetDDI_shutdown(UINT16_T port)


for each of the SDK functions that you wish to use (see the comprehensive PM3 Comms Interface Definition document for details)
e.g.

Code: Select all

 
tkcmdsetDDI_shutdown(port)


Example of a wrapper function definition

Code: Select all

 
PM3_WRAPPER_API ERRCODE_T _stdcall W_tkcmdsetDDI_shutdown(UINT16_T port)
{
return tkcmdsetDDI_shutdown(port);
} 


To use the wrapper.dll functions in VB you then simply declare them within your VB Code
e.g.

Code: Select all

 
Option Explicit

Private Declare Function W_tkcmdsetDDI_shutdown Lib "PM3_Wrapper.dll" _
 Alias "_W_tkcmdsetDDI_shutdown@4" _
 (ByVal port As Integer) As Integer
The most tricky part of the process if you are not familiar with compiling dlls is that you need the exported symbolic name of the functions we have defined within our wrapper.dll file.

In this case this is

Code: Select all

"_W_tkcmdsetDDI_shutdown@4"
The symbolic names are written to the map file which is generated when the dll is compiled. (Make sure that Generate Mapfile is selected when building a dll for use within a VB Project)

Here are some lines from the mapfile in this example

Code: Select all

0001:000001b0       _W_tkcmdsetDDI_discover_pm3s@12 100011b0 f   PM3_Wrapper.obj
0001:00000210       _W_tkcmdsetDDI_shutdown@4  10001210 f   PM3_Wrapper.obj
0001:00000270       _W_tkcmdsetDDI_fw_version@8 10001270 f   PM3_Wrapper.obj
Note that this is where the "_W_tkcmdsetDDI_shutdown@4" comes from and that in this example the wrapper dll is called PM3_Wrapper

I hope that this is of some use.

haboustak
500m Poster
Posts: 77
Joined: March 17th, 2006, 3:02 pm
Location: Cincinnati

Post by haboustak » May 25th, 2006, 12:34 pm

Great summary of the VB-support issues.

If you want to export non-mangled names in your VB code, you can add a .DEF file to your VC++ wrapper DLL project.

Something like....

Code: Select all

LIBRARY PM3_Wrapper
EXPORTS
    W_tkcmdsetDDI_discover_pm3s = _W_tkcmdsetDDI_discover_pm3s@12
Then you can Declare W_tkcmdsetDDI_discover_pm3s rather than using the mangled name in VB.

Mike

James Ball
Paddler
Posts: 3
Joined: January 27th, 2015, 6:33 am

Re: Visual Basic development

Post by James Ball » February 13th, 2015, 5:47 am

Haboustak, would it be possible for you to supply those import lines? I am asking because I have what I think SHOULD work but I am getting:

"An unhandled exception of type 'System.AccessViolationException' occurred in DLLTest.exe

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt"

as an error when I try to do anything but initialise the dll.

If possible could you also point me in the right direction as to the wrapper dll as I am willing to try that but am feeling very lost atm.

Bob S.
Marathon Poster
Posts: 5142
Joined: March 16th, 2006, 12:00 pm

Re: Visual Basic development

Post by Bob S. » February 13th, 2015, 11:34 am

Look at the date on the previous post. This thread has been dead for almost 9 years.

Bob S.

MarkD
Paddler
Posts: 5
Joined: December 7th, 2014, 8:02 pm
Location: Sammamish, WA or Reading, UK
Contact:

Re: Visual Basic development

Post by MarkD » February 13th, 2015, 2:32 pm

Make sure you are handling byref and byval parameters correctly. My suspicion is that, because the example you were given was a byval parameter, then you may have declared all of them byval. Return parameters need to be implicitly (e.g an array) or explicitly (e.g. a ushort) declared as byref.

Here is a C# example of parameter that are implicitly byref (rsp_data) or need to be declared explicitly byref (rsp_data_size).

They also have marshalling attributes ([in] [out], etc) for explicit byref parameters.

I don't know enough about VB to help you with those language specifics but hopefully the C# will help get you on the right path.

Code: Select all


        [DllImport("ExternalDlls\\PM3CsafeCP.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "?tkcmdsetCSAFE_command@@YAFGGQAKPAG0@Z")]
        public static extern ushort tkcmdsetCSAFE_command(
           [In] ushort unit_address,
           [In] ushort cmd_data_size,
           [In] uint[] cmd_data,
           [In, Out] ref ushort rsp_data_size,
           [In] uint[] rsp_data);

User avatar
Citroen
SpamTeam
Posts: 8059
Joined: March 16th, 2006, 3:28 pm
Location: A small cave in deepest darkest Basingstoke, UK

Re: Visual Basic development

Post by Citroen » February 13th, 2015, 3:43 pm

Bob S. wrote:Look at the date on the previous post. This thread has been dead for almost 9 years.

Bob S.
And Mike hasn't logged in since 2013.

James Ball
Paddler
Posts: 3
Joined: January 27th, 2015, 6:33 am

Re: Visual Basic development

Post by James Ball » February 23rd, 2015, 8:07 am

Sorry for the late reply. Thank you very much MarkD, the ByRef ByVal parameters were the issue! I know the post (and the forum really) had been dead for a long time but I was in need of the help!

Post Reply