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);
Related
Here is my attempt and error:
code:
TCHAR* pszDisplayName_I; //$I .. file
TCHAR *pDollar = NULL;
pszDisplayName_I = ((CString)saSursa.GetAt(i)).GetBuffer();
pDollar = _tcsrchr(pszDisplayName_I,'$');
*(pDollar + 1) = 'I';
error:
error C2248: 'CObject::CObject' : cannot access private member declared in class 'CObject' C:\Program Files\Microsoft Visual Studio 9.0\VC\atlmfc\include\afxcoll.h
for the getBuffter function I found this:
Returns a pointer to the internal character buffer for the CString object. The returned LPTSTR is not const and thus allows direct modification of CString contents.
So it is ok.
But for the getAt(0) function it says this:
CObject* GetAt(
INT_PTR nIndex
) const;
So there it is, the const thing, is there a way around this? A way in which I might get a modifiable CString element from a CStringArray.
I just compiled this successfully. Instead using GetAt() I used the [] operator.
CStringArray saTest;
saTest.Add(_T("Test"));
TCHAR* pTest = saTest[0].GetBuffer();
saTest[0].ReleaseBuffer();
Can anyone help me with a way of converting String to double in vc++?
I can't use atoi, since it converts char to double. But I am using istringstream.
std::istringstream stm;
double d;
String name = "32.67";
stm.str(name);
stm >>d;
It will give a compilation error:
error C2664: 'void std::basic_istringstream::str(const std::basic_string &)' :
cannot convert parameter 1 from 'System::String ^' to 'const std::basic_string &'
Please help with different solution or correct this.
std::stringstream str() accepts a std::string as an argument. You're passing it a System::String, wherever that comes from. Given the funky ^ symbol you must be using C++/CLI, using .NET strings.
Use std::string unless you are for some reason required to use the .NET library, in which case you need to either use .NET conversion functions, or convert to a std::string (or char* c-string and use the << operator).
As the other responder has suggestion, you are probably using C++/CLI. In that case:
String ^ name = "32.67";
double d;
d = Double::Parse(name);
Note that if the string cannot be parsed into a double, an exception will be thrown. Use Double::TryParse if you want to avoid that (it returns false if the string cannot be parsed).
I think it is very simple in vc++/CLR programming.
String ^name = "32.56";
String ^no = "56";
Double number_double = Convert::ToDouble(name); // convert String to double
Int number_int = Convert::ToInt32(no); // convert String to integer
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;
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;
}
I want to write
int i=4;
textBox1->Text = i;
But it is giving compilation error for type mismatch. How to do box or typecast this?
Sorry for answering the quesition myself. But I just got it while searching.
There is a very easy method
int i=4;
textBox1->Text = Convert::ToString(i);
Instead you could use: textBox1->Text = i.ToString();.
There is a very easy method
int i=4;
textBox1->Text = ""+i;
You need conversion, not a cast. Use itoa() or itow() depending on whether you compile for Unicode.
if you are using CString you can use Format method, or use old c function itoa
example:
CString str;
str.Format("%d",i);
also do not forget to call UpdateData method to update the GUI controls
Convert integer to string and set as value for Text.
CString textVal;
textVal.Format(_T("%d"), i);
textBox1->Text = textVal;