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

Hybrid View

  1. #1
    Join Date
    Jun 2006
    Posts
    17

    [RESOLVED] String to Char *

    Hi group,

    Apologies, I am not a C++ programmer and do not understand the intricacies of pointers!

    I have a vb.net project (vb2005) which needs to call a C++ class module which I have added to the project. The C++ module accepts as input a char * variable, which has no equivalent in vb. On the vb side the variable is a string. I have successfully added and tested simple C++ calls and they all work fine, but I cannot get this to work, best I can do is to get vb to think its of type SByte.

    I'm told that in .Net all data types are compatible at some level. I need to somehow represent a string as char * in vb, or a char pointer as string in C++, but don't know how to do it or the best way to go about it.

    Basically I need to modify the C++ code to accept a string in the Public method and then internally to convert it to a char pointer, but don't know how to do it, not even sure what the string equivalent in C++ is.

    Thanks in advance...

  2. #2
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: String to Char *

    You can convert string to LPTSTR (or TCHAR) using LPCSTR operator:
    Code:
    	CString csTest(_T("Testt String"));
    
    	LPTSTR lpszBuff = (LPTSTR)(LPCTSTR)csTest;
    	TCHAR* pszBuff =  (LPTSTR)(LPCTSTR)csTest;
    But I think it will be more feasible to pass BSTR (_bstr_t) or VARIANT (_variant_t)
    Code snippet uses generic text mapping working for both: UNICODE and ASCII.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  3. #3
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: String to Char *

    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  4. #4
    Join Date
    Jun 2006
    Posts
    17

    Re: String to Char *

    Thanks very much for both your responses, its cleared it up for me!

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