Results 1 to 5 of 5

Thread: CString::GetLength

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2001
    Location
    Holland
    Posts
    15

    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

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2001
    Location
    Holland
    Posts
    15

    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.

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2001
    Location
    Holland
    Posts
    15

    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);
    }

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width