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

Unmanaged C dll call

I am trying to get a C DLL to work with VB.NET 2005. Below is the original
header, my conversion, and test code. There is no error, but
RVERSION_RESP.version returns empty. I must be missing something. Thanks
in advance for any help.
#define REQUEST_DECLINED -2

typedef struct rversion_parms_set_1 RVERSION_PARMS;
typedef struct rversion_resp_set_1 RVERSION_RESP;

#pragma pack(1)

struct rversion_parms_set_1
{
int size; /* REQUIRED Size of structure */
};

struct rversion_resp_set_1
{
int size; /* REQUIRED Size of structure */
int return_code; /* = 0 if no error, != 0 if error */
char return_text[200]; /* Contains error text if return code != 0 */
char version[11]; /* DLL version */
};
#pragma pack()
_declspec(dllexport) extern void rversion(RVERSION_PARMS *rversion_parms,
RVERSION_RESP *rversion_resp);

Module Module1
Public Structure TRVERSION_PARMS
Dim size As Integer ' REQUIRED Size of structure
End Structure

Public Structure TRVERSION_RESP
Dim size As Short ' REQUIRED Size of structure
Dim return_code As Short ' 0 if ( no error, != 0 if ( error
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=200)> _
Dim return_text() As Char ' Contains error text if return code !=
0
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=11)> _
Dim version() As Char ' DLL version
End Structure

Public RVERSION_PARMS As TRVERSION_PARMS
Public RVERSION_RESP As TRVERSION_RESP

Public Declare Function rversion Lib "c:\remotapi.dll" (ByRef
RVERSION_PARMS As TRVERSION_PARMS, ByRef RVERSION_RESP As TRVERSION_RESP) As
Integer

End Module
Test
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
RVERSION_PARMS.size = Marshal.SizeOf(RVERSION_PARMS)
RVERSION_RESP.size = Marshal.SizeOf(RVERSION_RESP)
rversion(RVERSION_PARMS, RVERSION_RESP)
If (RVERSION_RESP.return_code <> 0) Then
TextBox1.Text = "RVERSION ERROR: " & RVERSION_RESP.return_text
Else
TextBox1.Text = "RVERSION: " & RVERSION_RESP.version
End If
End Sub

Jan 4 '06 #1
5 1469
Hi Bob,

The int in C++ means a 32bit data type.
But the Short in VB.NET is 16bit data type.

So I think we need to change the definition in the VB.NET signature.
Here is what works at my side for your reference.
Public Structure TRVERSION_PARMS
Dim size As Integer ' REQUIRED Size of structure
End Structure

Public Structure TRVERSION_RESP
Dim size As Integer ' REQUIRED Size of structure
Dim return_code As Integer ' 0 if ( no error, != 0 if ( error
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=200)> _
Dim return_text() As Char ' Contains error text if return code !=
0
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=11)> _
Dim version() As Char ' DLL version
End Structure

Public RVERSION_PARMS As TRVERSION_PARMS
Public RVERSION_RESP As TRVERSION_RESP

Public Declare Function fnTestDLL Lib "TestDLL.dll" (ByRef
RVERSION_PARMS As TRVERSION_PARMS, ByRef RVERSION_RESP As TRVERSION_RESP)
As Integer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
RVERSION_PARMS.size = Marshal.SizeOf(RVERSION_PARMS)
RVERSION_RESP.size = Marshal.SizeOf(RVERSION_RESP)
MsgBox(fnTestDLL(RVERSION_PARMS, RVERSION_RESP).ToString())
If (RVERSION_RESP.return_code <> 0) Then
TextBox1.Text = "RVERSION ERROR: " & RVERSION_RESP.return_text
Else
TextBox1.Text = "RVERSION: " & RVERSION_RESP.version
End If
End Sub
[C++]
TESTDLL_API int fnTestDLL(RVERSION_PARMS *rversion_parms, RVERSION_RESP
*rversion_resp)
{
rversion_resp->return_code = 0;
strcpy(rversion_resp->return_text,"Hello");
strcpy(rversion_resp->version,"World");
return 42;
}
ifdef TESTDLL_EXPORTS
#define TESTDLL_API __declspec(dllexport)
#else
#define TESTDLL_API __declspec(dllimport)
#endif

// This class is exported from the TestDLL.dll
class TESTDLL_API CTestDLL {
public:
CTestDLL(void);
// TODO: add your methods here.
};

#define REQUEST_DECLINED -2

typedef struct rversion_parms_set_1 RVERSION_PARMS;
typedef struct rversion_resp_set_1 RVERSION_RESP;

#pragma pack(1)

struct rversion_parms_set_1
{
int size; /* REQUIRED Size of structure */
};

struct rversion_resp_set_1
{
int size; /* REQUIRED Size of structure */
int return_code; /* = 0 if no error, != 0 if error */
char return_text[200]; /* Contains error text if return code != 0 */
char version[11]; /* DLL version */
};
#pragma pack()
extern TESTDLL_API int nTestDLL;

extern "C" TESTDLL_API int fnTestDLL(RVERSION_PARMS *rversion_parms,
RVERSION_RESP *rversion_resp);

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Jan 5 '06 #2
I now get this response from the DLL;
Rversion response structure has bad size.

Here is the function call:
Public Declare Function rversion Lib "c:\remotapi.dll" (ByRef RVERSION_PARMS
As TRVERSION_PARMS, ByRef RVERSION_RESP As TRVERSION_RESP) As Integer

There is something going on a am unaware of, if I write the structure in C
or Delphi, it is 219 bytes long, and the DLL works. However in VB.net the
structure is 220. If I lower the SizeConst to 198 vise 199, the structure
size drops to 216. I tried using byte instead, and got the same result. I am
lost..... Notice the packed record in Delphi, and the #pragma pack(1) in the
C header. Could this have something to do with it?
Public Structure TRVERSION_RESP
Dim size As Integer ' REQUIRED Size of structure
Dim return_code As Integer ' 0 if no error, != 0 if error
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=199)> _
Dim return_text() As Char ' Contains error text if return code !=
0
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=10)> _
Dim version() As Char ' DLL version
End Structure
This stucture in Delphi as a size of 219 and works with the DLL
type
TRVERSION_RESP = packed record
size: Integer; // REQUIRED Size of structure
return_code: Integer; // 0 if no error, != 0 if error
return_text: array[0..199] of Char; // Contains error text if return
code != 0
version: array[0..10] of Char; // DLL version
end;
Here is the original C header declaration.
#pragma pack(1) struct rversion_resp_set_1
{
int size; /* REQUIRED Size of structure */
int return_code; /* = 0 if no error, != 0 if error */
char return_text[200]; /* Contains error text if return code != 0 */
char version[11]; /* DLL version */
};



Jan 5 '06 #3
Hi Bob,

Have you tried my code in my last post?
Because that works at my side, if we test on your side, it will help us to
isolate the problem.

For your comment about the size of the structure.
I think it may be align problem.
align (C++)
http://msdn2.microsoft.com/en-us/library/83ythb65.aspx

Also in .NET we have a similar attribute StructLayoutAttribute.Pack Field,
you may try to change them to see if that will help you.
http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfsystemruntimeinteropservicesstructlayoutattri buteclasspacktopic.asp

If that still did not works for you, can you send me a reproduce
sample?(C++ and VB.NET code including the source code, I am not familar
with delphi)
You can reach me by removing "online" from my email address.
Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Jan 6 '06 #4
I did run your example, and it worked. Thank you so much for your help. I
finally achieved success by adding the line below to the top of the
structure. You hit it right on the head, it was an align problem. The pack=1
solved it.

Successful Line added.

<StructLayout(LayoutKind.Sequential, Pack:=1, CharSet:=CharSet.Ansi)> _

""Peter Huang" [MSFT]" <v-******@online.microsoft.com> wrote in message
news:$f**************@TK2MSFTNGXA02.phx.gbl...
Hi Bob,

Have you tried my code in my last post?
Because that works at my side, if we test on your side, it will help us to
isolate the problem.

For your comment about the size of the structure.
I think it may be align problem.
align (C++)
http://msdn2.microsoft.com/en-us/library/83ythb65.aspx

Also in .NET we have a similar attribute StructLayoutAttribute.Pack Field,
you may try to change them to see if that will help you.
http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfsystemruntimeinteropservicesstructlayoutattri buteclasspacktopic.asp

If that still did not works for you, can you send me a reproduce
sample?(C++ and VB.NET code including the source code, I am not familar
with delphi)
You can reach me by removing "online" from my email address.
Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no
rights.

Jan 6 '06 #5
Hi Bob,

Thanks for your update.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Jan 9 '06 #6

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

Similar topics

4
by: Gnanaprakash Rathinam | last post by:
Hi Expert, Is there a way to obtain assembly name in an unmanaged call? During Interop call between managed to unmanaged, I would like to know in unmanaged code about the caller of assembly file...
1
by: Don | last post by:
I have a third party C++ DLL that I am trying to use from C#. The specific function I am trying to use is declared in C++ as follows: ladybugConvertToMultipleBGRU32( LadybugContext ...
2
by: joye | last post by:
Hello, My question is how to use C# to call the existing libraries containing unmanaged C++ classes directly, but not use C# or managed C++ wrappers unmanaged C++ classes? Does anyone know how...
4
by: Rachel Suddeth | last post by:
What is the difference between a managed/unmanaged resource, and how do you tell which is which? I'm trying to understand how to write some Dispose() methods, and we are supposed to put code that...
1
by: Sparhawk | last post by:
Hi, my company is going to migrate a large VC++ application to .NET to make use of Windows Forms (the old class library is not updated any more). We are not planning to migrate the rest of the...
6
by: marek | last post by:
Hello All, we are doing a quite a big project that contains at the lowest level an unmenaged c++ classes. Above it there are managed wrappers and at the top there are ASP.NET pages. Can anyone...
5
by: R. MacDonald | last post by:
Hello, all, I am currently working on a .Net (VB) application that invokes routines in unmanaged (Fortran) DLLs. The unmanaged routines then communicate with the .Net application by means of a...
9
by: Amit Dedhia | last post by:
Hi All I have a VC++ 2005 MFC application with all classes defined as unmanaged classes. I want to write my application data in xml format. Since ADO.NET has buit in functions available for...
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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.