Translating char variable type from C++

J

John Dann

I'm trying (in VB2005) to interact with a third-party DLL written (I
think) in C++.

One of the functions requires me to pass a structure consisting
solely of char types according to the DLL documentation, but AIUI in
C++ this will be single byte values not unicode as in .Net. Each
variable element of the srtucture will assume a simple integer value
of 0, 1, 2 or 3 - in other words digits only and no other characters.

I'm thinking that in VB2005 I should make the structure of byte type
elements. Anyone care to confirm/correct me please?
 
D

diAb0Lo

I'm trying (in VB2005) to interact with a third-party DLL written (I
think) in C++.

One of the functions requires me to pass a structure consisting
solely of char types according to the DLL documentation, but AIUI in
C++ this will be single byte values not unicode as in .Net. Each
variable element of the srtucture will assume a simple integer value
of 0, 1, 2 or 3 - in other words digits only and no other characters.

I'm thinking that in VB2005 I should make the structure of byte type
elements. Anyone care to confirm/correct me please?

Hi there,

Arrays and structures in c++ are always By Reference Parameters (I
Think). However, I dont understand very well what you are trying to do.
 
J

John Dann

Arrays and structures in c++ are always By Reference Parameters (I
Think). However, I dont understand very well what you are trying to do.

OK, let me try and explain in a little more detail:

I'm trying (in VB2005) to interact with a third-party DLL written (I
think) in C++, but almost certainly not in a .Net version of the
language. Therefore I'm having to use COM Interop Services. I can call
several of the functions in the DLL successfully so I know that my
approach is working OK in general.

However there's one particular DLL function that is refusing to work
correctly. This is a function that requires a structure passed to it.
The structure definition (from the C/C++ documentation) is:

struct MyStructure
{
char parA;
char parB;
char parC;
}

where parA, B, C etc seem to take simple integer values like 0, 1, 2,
3 (even though they're typed as char).

I'm trying to simulate this structure in VB2005 as in:

Public Structure MyDotNetStructure
Dim parA as Byte
Dim parB as Byte
Dim parC as Byte
End Structure

and then setting the values for the structure elements like:

MyDotNetStructure.parA=Asc("0")

(Using the Asc() function to simulate the value that the C++ char
might have, was my thinking.)

then calling the DLL function by

Dim Response as short = DLLName.MyFunction(ByRef MyDotNetStructure)

(MyFunction is declared as a shared function in a class in my project
called DLLName that imports the InterOpServices.)

But I keep getting an 'invalid data' response from the function. I
presume that I haven't got the values or type of the structure right
but I'm not sure what to try next - there are quite a few permutations
of ByRef/ByVal, byte values etc etc. I was hoping someone might be a
bit more familiar with how to code this sort of communication.

Anyone please?
 
A

Andrew Morton

John said:
where parA, B, C etc seem to take simple integer values like 0, 1, 2,
3 (even though they're typed as char).

But I keep getting an 'invalid data' response from the function.

Asc("0") is 48 - is that a valid number to send it?

If you want to set it to a number, set it to a number:
MyDotNetStructure.parA = 0

HTH

Andrew
 
J

John Dann

Asc("0") is 48 - is that a valid number to send it?

Yes I think that's the problem - thanks. This was actually how I had
it originally, but it wasn't working so I started to wonder whether
char wasn't more like a string type and hence added the Asc()
conversion. Possibly what stopped it working originally was that the
structure was being passed ByVal and not ByRef (as per diAb0Lo's
suggestion). But when I changed to ByRef I didn't then remove the
Asc() conversion again.
 
D

diAb0Lo

OK, let me try and explain in a little more detail:

I'm trying (in VB2005) to interact with a third-party DLL written (I
think) in C++, but almost certainly not in a .Net version of the
language. Therefore I'm having to use COM Interop Services. I can call
several of the functions in the DLL successfully so I know that my
approach is working OK in general.

However there's one particular DLL function that is refusing to work
correctly. This is a function that requires a structure passed to it.
The structure definition (from the C/C++ documentation) is:

struct MyStructure
{
char parA;
char parB;
char parC;

}

where parA, B, C etc seem to take simple integer values like 0, 1, 2,
3 (even though they're typed as char).

I'm trying to simulate this structure in VB2005 as in:

Public Structure MyDotNetStructure
Dim parA as Byte
Dim parB as Byte
Dim parC as Byte
End Structure

and then setting the values for the structure elements like:

MyDotNetStructure.parA=Asc("0")

(Using the Asc() function to simulate the value that the C++ char
might have, was my thinking.)

then calling the DLL function by

Dim Response as short = DLLName.MyFunction(ByRef MyDotNetStructure)

(MyFunction is declared as a shared function in a class in my project
called DLLName that imports the InterOpServices.)

But I keep getting an 'invalid data' response from the function. I
presume that I haven't got the values or type of the structure right
but I'm not sure what to try next - there are quite a few permutations
of ByRef/ByVal, byte values etc etc. I was hoping someone might be a
bit more familiar with how to code this sort of communication.

Anyone please?

Yet, I dont understand what your problem is. See, I did this and
worked ok:

Public Structure MyDotNetStructure
Dim parA As Byte
Dim parB As Byte
Dim parC As Byte
End Structure

Dim mySt as new MyDotNetStructure
mySt.parA = Asc("0")

At least parA is set to 48.
 
D

diAb0Lo

OK, let me try and explain in a little more detail:

I'm trying (in VB2005) to interact with a third-party DLL written (I
think) in C++, but almost certainly not in a .Net version of the
language. Therefore I'm having to use COM Interop Services. I can call
several of the functions in the DLL successfully so I know that my
approach is working OK in general.

However there's one particular DLL function that is refusing to work
correctly. This is a function that requires a structure passed to it.
The structure definition (from the C/C++ documentation) is:

struct MyStructure
{
char parA;
char parB;
char parC;

}

where parA, B, C etc seem to take simple integer values like 0, 1, 2,
3 (even though they're typed as char).

I'm trying to simulate this structure in VB2005 as in:

Public Structure MyDotNetStructure
Dim parA as Byte
Dim parB as Byte
Dim parC as Byte
End Structure

and then setting the values for the structure elements like:

MyDotNetStructure.parA=Asc("0")

(Using the Asc() function to simulate the value that the C++ char
might have, was my thinking.)

then calling the DLL function by

Dim Response as short = DLLName.MyFunction(ByRef MyDotNetStructure)

(MyFunction is declared as a shared function in a class in my project
called DLLName that imports the InterOpServices.)

But I keep getting an 'invalid data' response from the function. I
presume that I haven't got the values or type of the structure right
but I'm not sure what to try next - there are quite a few permutations
of ByRef/ByVal, byte values etc etc. I was hoping someone might be a
bit more familiar with how to code this sort of communication.

Anyone please?

Yet, I dont understand what your problem is. See, I did this and
worked ok:

Public Structure MyDotNetStructure
Dim parA As Byte
Dim parB As Byte
Dim parC As Byte
End Structure

Dim mySt as new MyDotNetStructure
mySt.parA = Asc("0")

At least parA is set to 48.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top