Click to See Complete Forum and Search --> : cannot convert parameter 1 from 'System::String __gc *' to 'LPCSTR'
ee01ppa
January 16th, 2006, 07:52 AM
Hi,
I am trying to pass comport name via combobox in managed c++.net but cant seen to get it to work. It returns with the erro message
cannot convert parameter 1 from 'System::String __gc *' to 'LPCSTR'
Can someone help me
This is my function.
String * commPort = commPortsBox->Items->Item[i]->ToString();
HANDLE hCom= ::CreateFile(commPort,GENERIC_WRITE | GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
Thanks.
ee01ppa
January 16th, 2006, 08:45 AM
I have managed to get it working.
System::String * str = commPortsBox->Items->Item[i]->ToString();
char* str2 = (char*)(void*)Marshal::StringToHGlobalAnsi(str);
HANDLE hCom= ::CreateFile(str2 ,GENERIC_WRITE | GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
Thanks.
cilu
January 16th, 2006, 11:11 AM
See this article http://support.microsoft.com/default.aspx?scid=kb;EN-US;311259
darwen
January 16th, 2006, 11:43 PM
I have managed to get it working.
System::String * str = commPortsBox->Items->Item[i]->ToString();
char* str2 = (char*)(void*)Marshal::StringToHGlobalAnsi(str);
HANDLE hCom= ::CreateFile(str2 ,GENERIC_WRITE | GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
Firstly, you're not releasing the memory you're allocating - and even worse it's global memory causing a global memory leak.
Secondly, this is a huge amount of effort for what is effectively a very simple operation.
See my article here. (http://www.codeguru.com/Cpp/Cpp/cpp_managed/moving/article.php/c8031/)
Darwen.
ee01ppa
January 17th, 2006, 02:52 AM
@ darwen
I have changed the code to as per your suggestion. 'Marshal::FreeHGlobal(commName)' has been used to free the memory. Is there anything wrong with this code as it stands?
System::String * comm = commPortsBox->SelectedItem->ToString();
char* commName = (char*)(void*)Marshal::StringToHGlobalAnsi(comm);
HANDLE hCom= ::CreateFile(commName,GENERIC_WRITE | GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
Marshal::FreeHGlobal(commName);
darwen
January 17th, 2006, 04:59 AM
It's ok as it stands however I prefer to do it as I've said in the article as this doesn't use global memory.
Darwen.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.