|
-
Dec 8th, 2005, 06:53 AM
#1
Thread Starter
Junior Member
string from dll truncated
I'm using a c++ DLL, which takes as a parameter a structure from VB, like this:
Public Type ODATA
lReadError As Long
szData As String * 128
End Type
I have declared the function as ByRef pOData As ODATA).
The problem is that when c-function returns the filled structure, the string part is not as it should. The first bytes have been dissappeared. If that function is used from another c code it returns correctly everything but not if called from VB. For example calling from c it returns a string "abcdefg", but called from VB it gets only the value "efg". I think it has something to do with VB way of handling strings with two bytes for pointer and length in the beginning.
How should I use/declare variables to receive the whole string?
The C DLL is not made specially for VB. I use VB 5.
Thanks in advance.
/petri
-
Dec 8th, 2005, 12:57 PM
#2
Frenzied Member
Re: string from dll truncated
check out this thread, parts of the replies may have the answer to your problem.
http://www.vbforums.com/showthread.php?t=354844
-
Dec 10th, 2005, 07:12 AM
#3
Thread Starter
Junior Member
Re: string from dll truncated
Thanks benmartin101
I see what you mean, but unfortunately that did not solve the problem.
I tried to give a pointer in long instead of a string:
VB Code:
Public Type OIDATA
lReadError As Long
szData As [COLOR=Red]long [/COLOR]
End Type
and then use it like this:
VB Code:
Temp = Space(255)
pOIData.szData = StrPtr(Temp)
retdata = OCIRead(lObject, lDevice, lIndex, pOIData)
But then I get an exception error and program terminates.
I think it has something to do with declare. The function is declared as
VB Code:
Declare Function OCIRead Lib "oci200.dll" (ByVal lOIObject As Long, lDevice As Long, ByVal lIndex As Long, [COLOR=Red]ByRef pOIData As OIDATA[/COLOR]) As Long
and now I should pass a value inside a structure which must be declared Byref.
Any ideas?
/petri
-
Dec 10th, 2005, 05:03 PM
#4
Re: string from dll truncated
try this
VB Code:
Public Type ODATA
lReadError As Long
szData(0 to 127) As Byte
End Type
Now the byte array should contain the string you are looking for.
-
Dec 13th, 2005, 12:08 PM
#5
Frenzied Member
Re: string from dll truncated
did you make this dll? Because I think its giving an error because perhaps it expects a string member in the OIDATA type.
-
Dec 13th, 2005, 04:11 PM
#6
Thread Starter
Junior Member
Re: string from dll truncated
moeur, I tried that, but got Type mismatch. Maybe I'm not handling right the Byte pointer now, as I need to convert that back to string again. Need to look at it closer tomorrow.
benmartin101, no that dll is not made by me (I wouldn't have this problem if I've done that ). It is part of the house/heat controlling system that has PC interface through a serial port.
Thanks for your help!
-
Dec 13th, 2005, 04:26 PM
#7
Thread Starter
Junior Member
Re: string from dll truncated
OK. I had a stupid mistake but could now process it.
Unfortunately the result was the same as before: the first four characters are missing, even if declared as byte (1 to 127).
When I use this same DLL with executable made with C the result was
"-1323827200,11,1", but call from VB gives still "3827200,11,1".
The big number with minus sign is in big-endian form and I must convert it to small endian. That is why I need all the characters (to get the energy consumption).
/petri
-
Dec 13th, 2005, 04:47 PM
#8
Re: string from dll truncated
show me how it is declared in C and how you are processing the string after you get it.
-
Dec 15th, 2005, 09:14 AM
#9
Thread Starter
Junior Member
Re: string from dll truncated
This is relevant (I think) code from C
Code:
#define _EXPORT(return_value) extern "C" return_value __stdcall __export
#pragma pack(1)
struct OIDATA
{
long lData[32];
long lCheckAlarm;
long lCategory;
long lReadError;
char szData[128]; // raw index read data as comma separated
};
_EXPORT(long) OCIRead(long lOIObject,long lDevice,long lIndex,struct OIDATA *pOIData);
and an example how I use data in VB
VB Code:
CopyMemory Temp, StrPtr(pOIData.szData), 15
Temp = Left(Temp, InStr(Temp, ",") - 1)
MsgBox Temp 'this is where the four leading characters are missing!
MYREC.Energy = ntohl(Val(Temp))
As said earlier, the result was the same even if szData was a string, not a Byte array. Then of course I didn't use CopyMemory() and StrPtr() functions.
Does this help you, moeur?
/petri
-
Dec 15th, 2005, 11:10 AM
#10
Re: string from dll truncated
That C code should be declared like this
VB Code:
Type OIDATA
lData(0 To 31) As Long
lCheckAlarm As Long
lCategory As Long
lReadError As Long
szData(0 To 137) As Byte ' raw index read data as comma separated
End Type
Declare Function OCIRead Lib "oci200.dll" (ByVal lOIObject As Long, ByVal lDevice As Long, ByVal lIndex As Long, pOIData As OIDATA) As Long
-
Dec 15th, 2005, 12:30 PM
#11
Thread Starter
Junior Member
Re: string from dll truncated
GREAT! It works now.
Thanks a lot, moeur
/petri
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|