-
CString::GetLength
Hello,
I've a quick question:
How can I determen the length of a string passed from a VB program. I started out with a verry simple function that returns the lenth of a string. (The equvilant of Len() in vb). But it does'nt work.
int test(CString input)
{
return input.GetLength();
}
and the VB code:
Declare Function test Lib "myDll" (ByVal str As String) As Integer
Private Sub Command1_Click()
MsgBox test("1234")
End Sub
The result is a messagebox containing "-6038" ?!?!?!?
I'm (developing) with Embedded visual studio 3.0
-
You can't use a CString object as parameter. You need LPSTR (or char* if you prefer that).
Code:
int test(char* input)
{
CString strInput(input);
return strInput.GetLength();
}
and the vb must be:
Declare Function test Lib "myDll" (ByVal str As String) As Long
Integer in VB is 16 bits long, in Windows C it is 32 bit.
-
Got it working :) tanks
hmm, it is working now. LPSTR did'nt work, but LPCWSTR did :)
int test(LPCWSTR input)
{
CString strInput(input);
return strInput.GetLength();
}
It seems that the passed string is a Unicode String or something. 2 bytes per character.
maybe there is a difference between Embedded VB and VB.
-
assign value
Ok that It works :)
But now:
How can I assign a value to the string? I'm trying to read a part of a file:
(this does'nt work)
WORD readString (WORD fileIndex,LPWSTR input,WORD len)
{
int readCount;
unsigned short *readBuffer;
CString strInput(input);
if(files[fileIndex].open)
{
if(len > inpBufSize) len=inpBufSize;
readBuffer=strInput.GetBuffer(len);
readCount = files[fileIndex].file.Read(readBuffer,len);
return readCount;
}
else return -1;
}
My function to write a vaulue allready works:
void writeString (WORD fileIndex, LPCWSTR string)
{
CString strInput(string);
// Create a storing archive.
CArchive arStore(&files[fileIndex].file, CArchive::store);
arStore.WriteString(strInput);
}
-
Why are you bringing MFC into this? Surely it's simpler just to stick with the standard C (or if you want, C++, but be careful with your function names) libraries to do all this? They can do it smaller than MFC can :confused: