473,383 Members | 1,735 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,383 software developers and data experts.

Trouble with 3rd party dll wrapper class

Hey All,

This is probably a easy question, but I'm really stuck. I have a
command console program written in C++, that I want to convert it to C#
to make it into a windows app. The problem is that my program uses a
third part dll. So I tried to write a wrapper class for it. Even
though I think my interface looks right... the values being returned
from the third party imported functions are off.

Original Code:

#define USBCAN_ANY_MODULE = 255;

// *********Function prototypes from 3rd party header
BYTE PUBLIC UcanInitHardware( byte* handle, byte deviceNum, byte
callType );
BYTE PUBLIC UcanGetHardwareInfo( byte handle, tUcanHardwareInfo* info
);

//*********Structures from 3rd party header
typedef struct
{
BYTE m_bDeviceNr;
BYTE m_UcanHandle;
DWORD m_dwReserved;
BYTE m_bBTR0;
BYTE m_bBTR1;
BYTE m_bOCR;
DWORD m_dwAMR;
DWORD m_dwACR;

// new values since 17.03.03 Version V2.16
BYTE m_bMode;
DWORD m_dwSerialNr;

} tUcanHardwareInfo;

//*********Original code fragment from main()
main()
{
byte handle;
tUcanHardwareInfo hardwrMsg;

UcanInitHardware ( &handle, USBCAN_ANY_MODULE, NULL );
UcanGetHardwareInfo( &handle, hardwrMsg)
}

// *********Original results
m_bDeviceNr = 0;
m_UcanHandle = 1;
m_dwReserved = N/A;
m_bBTR0 = 1;
m_bBTR1 = 28;
m_bOCR = 26;
m_dwAMR = 0xFFFFFFFF;
m_dwACR = 0;

// new values since 17.03.03 Version V2.16
m_bMode = 0;
m_dwSerialNr = 0x5FFE7;

//********* Here's my attempt at importing the DLL functions
public const int USBCAN_ANY_MODULE = 255;

[DllImport("USBCAN32.dll", EntryPoint="UcanInitHardware" )]
public static extern byte UcanInitHardware( ref byte handle, byte
DeviceNum, byte fpCallbackFkt_p);

[DllImport("USBCAN32.dll" )]
public static extern byte UcanGetHardwareInfo(byte UcanHandle_p, ref
Structures.tUcanHardwareInfo pHwInfo_p);

[ StructLayout( LayoutKind.Sequential )]
public struct tUcanHardwareInfo
{
public byte m_bDeviceNr;
public byte m_UcanHandle;
public uint m_dwReserved;
public byte m_bBTR0;
public byte m_bBTR1;
public byte m_bOCR;
public uint m_dwAMR;
public uint m_dwACR;

// new values since 17.03.03 Version V2.16
public byte m_bMode;
public uint m_dwSerialNr;
}

//********* New main()
main()
{
byte UcanHandle = 0;
Structures.tUcanHardwareInfo hardwrMsg = new
Structures.tUcanHardwareInfo();

USBCAN32.UcanInitHardware(ref UcanHandle, USBCAN_ANY_MODULE,
0);
USBCAN32.UcanGetHardwareInfo( UcanHandle, ref hardwrMsg );

}

//********* New results
m_bDeviceNr = 0;
m_UcanHandle = 1;
m_dwReserved = N/A;
m_bBTR0 = 26;
m_bBTR1 = 0xFF;
m_bOCR = 0xFF;
m_dwAMR = 0xFFE7;
m_dwACR = 0xFF;

// new values since 17.03.03 Version V2.16
m_bMode = 5;
m_dwSerialNr = 0;
It seems that the results from "UcanGetHardwareInfo" are not being
returned into the correct fields of structure tUcanHardwareInfo. I
also noticed that if I put the 5 of variable m_bMode with m_dwAMR, it
could = 0x5FFE7 .... which is the expected value of m_dwSerialNr. So
does anyone have any idea what I'm doing wrong for the structure's
fields to get so confused ?

Jan 11 '06 #1
1 3164
Hi Mercy.

I think you might be experiencing a problem with byte alignment.

When you comile C++ code, the compiler allows you to specify how you want to
align your data (i.e. byte, word, doubleword, quadword... ). For instance,
if you specify word, the compiler try to allocate every field of a structure
at an even memory address, so if your structure contains a char and an int,
the compiler will insert an empty char of padding after the byte. If you
chose double word, the compiler try to allocate everything at a memory
address multiple of four and so on...
This is done for efficiency reasons, which are related to the design of the
CPU and the way it accesses memory and cache. This is a long story which
won't go into, but you can read the Assembler guides available on Intel web
site if you want to know more. Or search goolge for "cache split fault",
whihc will give an idea.

However, in your code you specify "layoutsequantial". This is equivakent to
byte alignment, but clearly this is not the way your dll was compiled. You
need to reverse engineer what is the correct alignment to be used.

Cheers,
Fabio
"Mercy" <me*********@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Hey All,

This is probably a easy question, but I'm really stuck. I have a
command console program written in C++, that I want to convert it to C#
to make it into a windows app. The problem is that my program uses a
third part dll. So I tried to write a wrapper class for it. Even
though I think my interface looks right... the values being returned
from the third party imported functions are off.

Original Code:

#define USBCAN_ANY_MODULE = 255;

// *********Function prototypes from 3rd party header
BYTE PUBLIC UcanInitHardware( byte* handle, byte deviceNum, byte
callType );
BYTE PUBLIC UcanGetHardwareInfo( byte handle, tUcanHardwareInfo* info
);

//*********Structures from 3rd party header
typedef struct
{
BYTE m_bDeviceNr;
BYTE m_UcanHandle;
DWORD m_dwReserved;
BYTE m_bBTR0;
BYTE m_bBTR1;
BYTE m_bOCR;
DWORD m_dwAMR;
DWORD m_dwACR;

// new values since 17.03.03 Version V2.16
BYTE m_bMode;
DWORD m_dwSerialNr;

} tUcanHardwareInfo;

//*********Original code fragment from main()
main()
{
byte handle;
tUcanHardwareInfo hardwrMsg;

UcanInitHardware ( &handle, USBCAN_ANY_MODULE, NULL );
UcanGetHardwareInfo( &handle, hardwrMsg)
}

// *********Original results
m_bDeviceNr = 0;
m_UcanHandle = 1;
m_dwReserved = N/A;
m_bBTR0 = 1;
m_bBTR1 = 28;
m_bOCR = 26;
m_dwAMR = 0xFFFFFFFF;
m_dwACR = 0;

// new values since 17.03.03 Version V2.16
m_bMode = 0;
m_dwSerialNr = 0x5FFE7;

//********* Here's my attempt at importing the DLL functions
public const int USBCAN_ANY_MODULE = 255;

[DllImport("USBCAN32.dll", EntryPoint="UcanInitHardware" )]
public static extern byte UcanInitHardware( ref byte handle, byte
DeviceNum, byte fpCallbackFkt_p);

[DllImport("USBCAN32.dll" )]
public static extern byte UcanGetHardwareInfo(byte UcanHandle_p, ref
Structures.tUcanHardwareInfo pHwInfo_p);

[ StructLayout( LayoutKind.Sequential )]
public struct tUcanHardwareInfo
{
public byte m_bDeviceNr;
public byte m_UcanHandle;
public uint m_dwReserved;
public byte m_bBTR0;
public byte m_bBTR1;
public byte m_bOCR;
public uint m_dwAMR;
public uint m_dwACR;

// new values since 17.03.03 Version V2.16
public byte m_bMode;
public uint m_dwSerialNr;
}

//********* New main()
main()
{
byte UcanHandle = 0;
Structures.tUcanHardwareInfo hardwrMsg = new
Structures.tUcanHardwareInfo();

USBCAN32.UcanInitHardware(ref UcanHandle, USBCAN_ANY_MODULE,
0);
USBCAN32.UcanGetHardwareInfo( UcanHandle, ref hardwrMsg );

}

//********* New results
m_bDeviceNr = 0;
m_UcanHandle = 1;
m_dwReserved = N/A;
m_bBTR0 = 26;
m_bBTR1 = 0xFF;
m_bOCR = 0xFF;
m_dwAMR = 0xFFE7;
m_dwACR = 0xFF;

// new values since 17.03.03 Version V2.16
m_bMode = 5;
m_dwSerialNr = 0;
It seems that the results from "UcanGetHardwareInfo" are not being
returned into the correct fields of structure tUcanHardwareInfo. I
also noticed that if I put the 5 of variable m_bMode with m_dwAMR, it
could = 0x5FFE7 .... which is the expected value of m_dwSerialNr. So
does anyone have any idea what I'm doing wrong for the structure's
fields to get so confused ?

Jan 11 '06 #2

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

Similar topics

12
by: Egil M?ller | last post by:
Is there any way to create transparent wrapper objects in Python? I thought implementing __getattribute__ on either the wrapper class or its metaclass would do the trick, but it does not work for...
10
by: gregory_may | last post by:
I have an application I created called "JpegViewer.exe". It simply loads a Jpeg file and displays in on the screen. It works great, in my lab. When I am using it at a customer site, things...
4
by: Jim Bancroft | last post by:
Hi everyone, I've recently ported a COM component into .Net using a runtime callable wrapper. The component in question is SQL Server's DTS package, if that helps. The import worked well...
1
by: Lee Greco | last post by:
A third party vendor has provided me with a DLL authored in C++ .Net. The vendor's package includes the DLL, .lib and all necessary .h files and sample code to develop a C++ .NET app. For the C++...
0
by: tsmith | last post by:
Hi Can somebody help me, please! I'm trying to use a third-party .NET forms control in an unmanaged COM-aware application (MFC). I have no problem doing this with normal .net controls, as...
0
by: ronnotel | last post by:
I have integrated APIs from a third party into my framework. The third party is essentially a .Net wrapper on top of an existing set of C++ classes and structs. I want to pass-around objects in...
6
by: =?Utf-8?B?TWFyaw==?= | last post by:
Hi... Tried posting this on Build Controls but then saw that's a pretty slow group... Kind of a typical request from product management - they want to be able to swap in different 3rd party...
1
by: jeddiki | last post by:
Hi, I am having difficulty seeing why my divs will not swap places :confused: I want to swap the positions of the "Hot News" and the "Todays Bonuese" sections. Here is how they look now: ...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.