开发者

How to convert a void * to CString

开发者 https://www.devze.com 2023-03-26 09:18 出处:网络
Not much of a C++ developer and the multiple ways to handle strings a开发者_Go百科lways confuses me.

Not much of a C++ developer and the multiple ways to handle strings a开发者_Go百科lways confuses me.

int Mine_SSL_Read(SSL* ssl, void* buf, int size)
{
    int length = Real_SSL_Read(ssl, buf, size);

    CString msg = ???
}

However I need to write a hook for SSL_Read function (OpenSSL) and that requires some C++ code. I need to convert buf which is of type void* and has a length of "length" into a CString so it can be parsed by other code.


Assuming the void* is simply one-byte (ASCII or similar) characters:

If you know it is NULL terminated, you can simply cast it:

// ASCII
CString msg = reinterpret_cast<char*>(buf);

// UNICODE
CString msg = reinterpret_cast<wchar_t*>(buf);

If it is not NULL terminated, or you don't know that fact, then you have to copy it byte by byte (I don't believe CString has an assign function similar to std::string).

CString msg;

char* str_buf = msg.GetBuffer();
char* msg_buf = reinterpret_cast<char*>(buf);

for(int x = 0; x < size; ++x)
    *str_buf++ = *msg_buf++;


You can try using reinterpret_cast,

CString msg = reinterpret_cast<wchar_t*>(buf);


it depends on what's in void buff. if it's char* a simple cast and CString constructor it's sufficient, otherwise you should convert wchar_t* to char*. Be sure that buffer is null terminated.

0

精彩评论

暂无评论...
验证码 换一张
取 消