This is my first VB.NET post as I'm mainly a C/C++ and PowerBASIC developer. I'm trying to figure out how to pass a struct like so as a function parameter to/from a C/C++ or PowerBASIC dll using PInvoke of course in VB.NET. The struct in C looks like this...
struct MAXTransItem //whatever...
{
int iItemNumber; //4 bytes
char szItemDescription[36]; //36 bytes
double dblCost; //8 bytes
};
This is just test code to see how to do it. I included an asciz null terminated char string buffer in the struct just because I knew that would be the most difficult. As you can see, that buffer is 36 bytes, and the whole struct would have a fixed memory footprint of 48 bytes.
Here is the whole dll code which just has one test function that I wish to access from VB.NET...
#include <string.h>
struct MAXTransItem //whatever...
{
int iItemNumber;
char szItemDescription[36];
double dblCost;
};
extern "C" __declspec(dllexport) int __stdcall ProcessTrans(int, MAXTransItem&);
int __stdcall ProcessTrans(int iQuantity, MAXTransItem& mti)
{
mti.iItemNumber=37;
strcpy(mti.szItemDescription,"Genuine Ash Axe Handles");
mti.dblCost=iQuantity*6.95;
return true;
}
If you are not familiar with C++ the 2nd parameter is a reference parameter, i.e.,
MAXTransItem&
meaning the address of an allocated MAXTransItem struct must be passed in. I've been working for quite some time at this and am pretty well stuck. However, just today I found a reference to something which might work in Francesco Balena's book "Programming Microsoft Visual Basic.NET" published by Microsoft Press around 2002 or so. He stated that this is VB.NET's closest approximation of a fixed length string as part of a Structure...
Structure
..
Dim zipcode As Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString
Sub New(.....)
zipcode=New Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString(36)
End Sub
....
End Structure
However, I've not been able to get it to work. I can't get it to compile with vbc. Here is my test program Test2.vb which I'm command line compiling with vbc...
Imports System
Imports Microsoft.VisualBasic
Imports System.Runtime.InteropServices
Module MainModule
Structure MAXTransItem
Public iItemNumber As Integer
Dim szDescription As Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString
Public dblCost As Double
Sub New()
szDescription=New Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString(36)
End Sub
End Structure
Sub Main()
Console.WriteLine("Hello, World!")
End Sub
End Module
Here are the error messages I'm getting with both v1.1 and v3.5 of vbc...
'C:\Code\MS.NET\VB.NET\Console\2nd>vbc Test2.vb
'Microsoft (R) Visual Basic .NET Compiler version 7.10.6001.4
'for Microsoft (R) .NET Framework version 1.1.4322.2443
'Copyright (C) Microsoft Corporation 1987-2002. All rights reserved.
'
'C:\Code\MS.NET\VB.NET\Console\2nd\Test2.vb(8) : error BC30002: Type 'Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString' is not defined.
'Dim szDescription As Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'C:\Code\MS.NET\VB.NET\Console\2nd\Test2.vb(11) : error BC30629: Structures cannot declare a non-shared 'Sub New' with no parameters.
'Sub New()
~~~
'C:\Code\MS.NET\VB.NET\Console\2nd\Test2.vb(12) : error BC30002: Type 'Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString' is not defined.
'szDescription=New Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString(36)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Code\MS.NET\VB.NET\Console\2nd>
Can anyone give me some direction on this? Is there a Imports directive I'm missing for the compiler to recognize the string...
'Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString'
Any other possible thoughts? I'm almost certain this can be done; I just don't know enough VB.NET to do it!