How to convert PUNICODE_STRING to bstr? - visual-c++

I'm calling a function which is in a ComVisible managed dll from a VC++ code. In the managed dll the function parameter type is string.
In the VC++ code i've the variable as PUNICODE_STRING. How can i pass it to the function? How can i convert it into BSTR?
Thank you.
NLV

The first thing to note is that PUNICODE_STRING's internal string buffer might not be null-terminated. So it would be best to go via a standard null-terminated wide string, that can then be passed straight to SysAllocString.
Try this:
BSTR PUNICODEToBSTR(PUNICODE_STRING pStr)
{
// create a null-terminated version of the input string
wchar_t* nullTerminatedString = new wchar_t[pStr->Length + 1];
memset(nullTerminatedString, 0, sizeof(wchar_t) * (pStr->Length + 1)];
wcsncpy(nullTerminatedString, pStr->Buffer, pStr->Length);
// create a BSTR
BSTR bstrString = ::SysAllocString(nullTerminatedString);
// tidy-up and return the BSTR
delete [] nullTerminatedString;
return bstrString;
}

Related

C++ Convert Sytem::String^ to LPCOLESTR

I write in mixed mode (C++/CLI) and I can not resolve this problem:
String ^progID = "Matrikon.OPC.Server";
CLSID clsid;
HRESULT result = CLSIDFromProgID(progID, &clsid);
error C2664: 'CLSIDFromProgID' : cannot convert parameter 1 from 'System::String ^' to 'LPCOLESTR'
How can I convert String^ to LPCOLESTR ?
Thanks!
I made another way:
// 1.
pin_ptr<const WCHAR> str = PtrToStringChars(progID);
LPCOLESTR coleString = (LPWSTR)str;
I have found that pin_ptr will be released if goes out of scope Define the Scope of Pinning Pointers and pin_ptr (C++/CLI)
This code works well for me:
// 2. this is the same like (1.)
String ^progID2 = "Matrikon.OPC.Simulation.1";// This is example of dynamic string
pin_ptr<const WCHAR> PINprogID2 = PtrToStringChars(progID2);
CLSID clsid2;
HRESULT result2 = CLSIDFromProgID(PINprogID2, &clsid2); //(LPCOLESTR, &CLSID)
Another example:
// 3.
pin_ptr<const WCHAR> sclsid3 = PtrToStringChars("{63D5F432-CFE4-11d1-B2C8-0060083BA1FB}");
CLSID clsid3;
CLSIDFromString((WCHAR*)sclsid3, &clsid3); //(LPOLESTR, &CLSID)
I am not much experienced and I am not sure if there are some lack of memory, but I think those codes are correct.
Avoid using the hammer for every nail. C++/CLI lets you just as easily use native types. So it is simply:
LPCOLESTR progid = L"Matrikon.OPC.Server";
// etc..
Non-zero odds (always say why) that you can simply use Type::GetTypeFromProgID().
First, lets convert System::String to char*
IntPtr p = Marshal::StringToHGlobalAnsi(progID);
char *pNewCharStr = static_cast<char*>(p.ToPointer());
second, casting char * to LPCOLESTR using ATL conversion macro:
LPCOLESTR converted_string = A2COLE(pNewCharStr);

How to convert string to LPSTR in WinAPI function which stores output in string

I am trying to store some contents into a string variable by passing it as a parameter in various types of Windows API functions which accepts variable like char *.
For example, my code is:-
std::string myString;
GetCurrentDirectoryA( MAX_PATH, myString );
Now how do I convert the string variable to LPSTR in this case.
Please see, this function is not meant for passing the contents of string as input, but the function stores some contents into string variable after execution. So, myString.c_str( ) is ruled out.
Edit: I have a workaround solution of removing the concept of string and replacing it with something like
char myString[ MAX_PATH ];
but that is not my objective. I want to make use of string. Is there any way possible?
Also casting like
GetCurrentDirectoryA( MAX_PATH, ( LPSTR ) myString );
is not working.
Thanks in advance for the help.
Usually, people rewrite the Windows functions they need to be std::string friendly, like this:
std::string GetCurrentDirectoryA()
{
char buffer[MAX_PATH];
GetCurrentDirectoryA( MAX_PATH, buffer );
return std::string(buffer);
}
or this for wide char support:
std::wstring GetCurrentDirectoryW()
{
wchar_t buffer[MAX_PATH];
GetCurrentDirectoryW( MAX_PATH, buffer );
return std::wstring(buffer);
}
LPTSTR is defined as TCHAR*, so actually it is just an ordinary C-string, BUT it depends on whether you are working with ASCII or with Unicode in your code. So do
LPTSTR lpStr = new TCHAR[256];
ZeroMemory(lpStr, 256);
//fill the string using i.e. _tcscpy
const char* cpy = myString.c_str();
_tcscpy (lpStr, cpy);
//use lpStr
See here for a reference on _tcscpy and this thread.
Typically, I would read the data into a TCHAR and then copy it into my std::string. That's the simplest way.

Visual C++ Write BSTR to File

I am a newbie to C++.
I am trying to write a BSTR to a CSV file, but the data when I print with wcout is not matching with data what's present in file.
BSTR tempString;
ofstream outputFile;
outputFile.open("C:\\data.csv",ios::out);
tempString = getData();
outputFile.write(tempString);
outputFile.close();
BSTR getData()
{
BSTR KBIDValue;
IStringCollection *KBID;
KBID->get_Item(0,&KBIDValue);
return KBIDValue;
}
If tempString = L"TestData" -> I am able to see the same value in
file.
If tempString = getData(); where the function returns a BSTR,
I am not able to see same value in file.
Could some please clarify this? Also please explain what exactly is the method to write BSTR to file?
EDIT: Added code
You are not returning anything!!!
BSTR getData()
{
BSTR KBIDValue;
IStringCollection *KBID;
KBID->get_Item(0,&KBIDValue);
return KBIDValue;
}
However if you want to convert to some other form and print, look here
I have added the following lines of code in my program and giving the results correctly.
ofstream outputFile;
outputFile.open(filePath,ios::out);
outputFile << W2A(CString(tempString));

C++/CLI String Conversions

I found this really nice piece of code that converts a string to a System:String^ as in:
System::String^ rtn = gcnew String(move.c_str()); // 'move' here is the string
I'm passing rtn back to a C# program. Anyways, inside the function where this code exists, I'm passing in a System::String^. I also found some code to convert a System:String^ to a string using the following code:
pin_ptr<const wchar_t> wch = PtrToStringChars(cmd); // 'cmd' here is the System:String
size_t convertedChars = 0;
size_t sizeInBytes = ((cmd->Length + 1) * 2);
errno_t err = 0;
char *ch = (char *)malloc(sizeInBytes);
err = wcstombs_s(&convertedChars,ch, sizeInBytes,wch, sizeInBytes);
Now I can use 'ch' as a string.
This, however, seems to be alot more work than converting the other way using the gcnew. So, at last my question is, is there something out there that will convert a System::String^ to string using a similar fashion as with the gcnew way?
Use VC++'s marshaling library: Overview of Marshaling in C++
#include <msclr/marshal_cppstd.h>
// given System::String^ mstr
std::string nstr = msclr::interop::marshal_as<std::string>(mstr);
this could be useful:
wchar_t *str = "Hi StackOverflow"; //native
String^ mstr= Marshal::PtrToStringAnsi((IntPtr)str); // native to safe managed
wchar_t* A=( wchar_t* )Marshal::StringToHGlobalAnsi(mstr).ToPointer(); // return back to native
don't forget using namespace System::Runtime::InteropServices;

Compare TCHAR with String value in VC++?

How to Compare TCHAR with String value in VC++ ?
My project is not Unicode.
I am doing like this :
TCHAR achValue[16523] = NULL;
if(achValue == _T("NDSPATH"))
{
return FALSE;
}
When achValue = "NDSPATH" then also this condition does not staisfy.
Any help is appreciated.
A TCHAR or any string array is simply a pointer to the first character. What you're comparing is the value of the pointer, not the string. Also, you're assigning an array to null which is nonsensical.
Use win32 variations of strcmp. If you use the _tcscmp macro, it will use the correct function for multibyte/unicode at compile time.
#define MAX_STRING 16523;
TCHAR achValue[MAX_STRING];
ZeroMemory(achValue, sizeof(TCHAR) * MAX_STRING);
sprintf(achValue, MAX_PATH, _T("NDSPATH"));
if (!_tcscmp(achValue, _T("NDSPATH"))
{
// strings are equal when result is 0
}

Resources