0

I use Turbo C++ explorer
i need to get file name from a TFileListBox
which is an AnsiString, and use it in CreateProcess
but CreateProcess Requires an "LPCTSTR" or "LPTSTR"
I would like to convert the ansistring to LPTSTR

Please help
thankx in advance

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by Alex Edwards
0

http://www.sscnet.ucla.edu/geog/gessler/borland/strings.htm

Also "LPCTSTR" or "LPTSTR" Looks like 'Long Pointer (const) to String' and 'Long Pointer to String'

Since CreateProcess is defined in windows.h, I'd assume that STR is referring to a char*, or c-style string.

You could create your own custom method for converting an AnsiString into a c_string--

Something along the lines of this--

#include <iostream>
#include <SysUtils.hpp>

using std::cin;
using std::cout;
using std::endl;
using std::flush;
using std::size_t;

const char* extractCString(const AnsiString&);

int main(){

    AnsiString temp ("Testing...");
    const char* myCString = extractCString(temp); // convert temp into a c_string
    delete myCString;
    cin.ignore();
    cin.get();
    return 0;
}

/**
 * Returns a new c_string based on the contents of the AnsiString
 */
const char* extractCString(const AnsiString& arg){

    /*
     * LOGIC:
     *      // Assume arg.Length() is 4, so length is 5
     *      -Dynamically create a new char* that spans length times sizeof(char) bytes in memory
     *      -For length - 1 (4) iterations, with i being our number for iteration, and repeat process while i < (length - 1) (i < 4), and increment i
     *          -Assign data[i] with the value stored in arg[i]
     *      -Assign a null terminator to the last value to make the last byte of the char array contain a null terminator, making it an official c_string
     *      -return the address of the first byte of the array
     */
    size_t length = arg.Length() + 1; // assign arg.Length() + 1 to variable length
    char* data = new char[length]; // assign address of a contiguous amount of memory this program is allowed to use, to this pointer

    for(size_t i = 0; i < (length - 1); i++) // repeat this process length - 1 times
        data[i] = arg[i]; // assign data[i] to be the value of arg[i]

    data[length - 1] = '\0'; // attach null terminator to last byte
    return data; // return the pointer
}

I couldn't compile the program because I don't own Turbo C++, nor do I own Borland C++ Builder, but the intent of this program is to encourage you to make your own conversion library for your purposes.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.