CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Char to LPCSTR

  1. #1
    Join Date
    Sep 2006
    Posts
    635

    Char to LPCSTR

    hi all

    how can convert of Char To LPCSTR,

    well, I am developing a DLL to use it in VB and this is a function that I am building.

    I try this, but it gives me bad result .
    Code:
    void LeerIni(LPCSTR *Valor, LPCSTR *Seccion, LPCSTR *Key, LPCSTR *FileIni
    , long *nCant)
    {
      char *res;
      GetPrivateProfileString(*Seccion, *Key,"-1", res, *nCant, *FileIni );
      *Valor=(LPCSTR )res; --here is the problem
    }
    DLL is created Ok.
    but when this function is used in VB, *Valor return anything less value I hope

    PDT. if anyone knows about develop DLL with C++ to user it in VB,
    could help me out with some links I can use like guide.

  2. #2
    Join Date
    Jun 2002
    Posts
    224

    Re: Char to LPCSTR

    Hello,

    I don't know about VB. It looks like you know the same thing about C++.

    There are too many mistakes in the code you posted. For instance:
    Code:
    char *res;
    GetPrivateProfileString(*Seccion, *Key,"-1", res, *nCant, *FileIni );
    When you declare a pointer to char it means you get exactly what you requested: a pointer to char; you do not get a pointer and space to store some characters. GetPrivateProfileString will try to store the result into the space pointed by res. But res points to a random location.

    You must read a bit (more) about C++. Sorry...
    ZDF

    What is good is twice as good if it's simple.
    "Make it simple" is a complex task.

  3. #3
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: Char to LPCSTR

    the function in your code does not allocate space for you..
    why put this in a function ? they are axaclty the same

  4. #4
    Join Date
    Sep 2006
    Posts
    635

    Re: Char to LPCSTR

    Quote Originally Posted by zdf
    Hello,

    I don't know about VB. It looks like you know the same thing about C++.

    There are too many mistakes in the code you posted. For instance:
    Code:
    char *res;
    GetPrivateProfileString(*Seccion, *Key,"-1", res, *nCant, *FileIni );
    When you declare a pointer to char it means you get exactly what you requested: a pointer to char; you do not get a pointer and space to store some characters. GetPrivateProfileString will try to store the result into the space pointed by res. But res points to a random location.

    You must read a bit (more) about C++. Sorry...
    thanks for reply, but I studied Borland c++ 3.0 before, but I have problem to develop DLL because I never have done it.

    I understand that you mean me. I have to initialize 'res'.
    Last edited by hensa22; February 14th, 2007 at 11:31 PM.

  5. #5
    Join Date
    Sep 2006
    Posts
    635

    Re: Char to LPCSTR

    Code:
    void LeerIni(LPCSTR *Valor, LPCSTR *Seccion, LPCSTR *Key, LPCSTR *FileIni
    , long nCant)
    {
      char *res;
      res=new char[nCant];
      GetPrivateProfileString(*Seccion, *Key,"-1", res, nCant, *FileIni );
      *Valor=(LPCSTR )res;
    }
    it's ok.
    it works perfectly in VB

    thanks.

  6. #6
    Join Date
    Jun 2002
    Posts
    224

    Re: Char to LPCSTR

    Hello,

    There are still errors in your code. For instance: LPCTSTR is a pointer to a vector of constant TCHAR. TCHAR is different from char, that is “*Valor=(LPCSTR )res;” is not all right.

    Don’t forget to release the memory.
    ZDF

    What is good is twice as good if it's simple.
    "Make it simple" is a complex task.

  7. #7
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: Char to LPCSTR

    LPCSTR is a pointer to memory which is used as an array of constant char's

    the code is perfectly fine.is this C or C++

  8. #8
    Join Date
    Jun 2002
    Posts
    224

    Re: Char to LPCSTR

    I am sorry but I do not understand. Why is it “perfectly fine”?
    ZDF

    What is good is twice as good if it's simple.
    "Make it simple" is a complex task.

  9. #9
    Join Date
    Sep 2006
    Posts
    635

    Re: Char to LPCSTR

    Quote Originally Posted by hensa22
    Code:
    void LeerIni(LPCSTR *Valor, LPCSTR *Seccion, LPCSTR *Key, LPCSTR *FileIni
    , long nCant)
    {
      char *res;
      res=new char[nCant];
      GetPrivateProfileString(*Seccion, *Key,"-1", res, nCant, *FileIni );
      *Valor=(LPCSTR )res;
    }
    this code above is still little bad.

    Code:
    long  LeerIni(LPSTR *Valor,const LPCSTR &Seccion, LPCSTR &Key, LPCSTR &FileIni,long &nCant)
    {
      long c=-1;
      c=GetPrivateProfileString(Seccion, Key,"-1",*Valor,nCant,FileIni);  
      return c;
    }
    it's at 100% ok.it works in VB without problem.

  10. #10
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: Char to LPCSTR

    oh man i missed that mistake, i hate microsoft defines they are so hard to detect mistakes.

    also why do make a temp var?
    just make Valor = new char instead of a temp

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

Featured