G
Guest
Hi
I’m trying to write a C++ class library in Visual Studio 7.1 .NET using
managed extensions that calls code in an unmanaged static library. Functions
in the static library have parameters that require the good ol’ char*. Since
string parameters to my managed class methods are .NET String*, I need to
port the String* to a char*. I tried doing this using the following code:
char *ChangeStr(String *cString)
{
IntPtr pPtr;
wchar_t *cWStr;
char *cStr;
pPtr =
System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(cString);
if (pPtr != NULL)
{
cWStr = (wchar_t*)pPtr.ToPointer();
size_t nSize = wcstombs(NULL, cWStr, 0) + 2;
cStr = (char *)malloc(nSize);
if (cStr == NULL)
{
throw __gc new
System::OutOfMemoryException(String::Format(S"malloc returned NULL for size
{0}.",__box(nSize)));
}
memset(cStr, '\0', nSize);
wcstombs(cStr, cWStr, nSize);
System::Runtime::InteropServices::Marshal::FreeCoTaskMem(pPtr);
return cStr;
}
return NULL;
}
I’m getting unresolved linking errors:
error LNK2001: unresolved external symbol "void * __cdecl memset(void
*,int,unsigned int)" (?memset@@$$J0YAPAXPAXHI@Z)
error LNK2001: unresolved external symbol "void * __cdecl malloc(unsigned
int)" (?malloc@@$$J0YAPAXI@Z)
error LNK2001: unresolved external symbol "unsigned int __cdecl
wcstombs(char *,unsigned short const *,unsigned int)"
(?wcstombs@@$$J0YAIPADPBGI@Z)
In my project:
Runtime Library is: Multi-threaded Debug (/MTd)
Ignore All Default Libraries is: No
I’m not sure why the CRT is not linking into the project. If anyone has any
suggestions on how to fix this or better yet, a managed way to convert my
String* to a char*, that would be great!
Thanks for any help you can provide.
I’m trying to write a C++ class library in Visual Studio 7.1 .NET using
managed extensions that calls code in an unmanaged static library. Functions
in the static library have parameters that require the good ol’ char*. Since
string parameters to my managed class methods are .NET String*, I need to
port the String* to a char*. I tried doing this using the following code:
char *ChangeStr(String *cString)
{
IntPtr pPtr;
wchar_t *cWStr;
char *cStr;
pPtr =
System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(cString);
if (pPtr != NULL)
{
cWStr = (wchar_t*)pPtr.ToPointer();
size_t nSize = wcstombs(NULL, cWStr, 0) + 2;
cStr = (char *)malloc(nSize);
if (cStr == NULL)
{
throw __gc new
System::OutOfMemoryException(String::Format(S"malloc returned NULL for size
{0}.",__box(nSize)));
}
memset(cStr, '\0', nSize);
wcstombs(cStr, cWStr, nSize);
System::Runtime::InteropServices::Marshal::FreeCoTaskMem(pPtr);
return cStr;
}
return NULL;
}
I’m getting unresolved linking errors:
error LNK2001: unresolved external symbol "void * __cdecl memset(void
*,int,unsigned int)" (?memset@@$$J0YAPAXPAXHI@Z)
error LNK2001: unresolved external symbol "void * __cdecl malloc(unsigned
int)" (?malloc@@$$J0YAPAXI@Z)
error LNK2001: unresolved external symbol "unsigned int __cdecl
wcstombs(char *,unsigned short const *,unsigned int)"
(?wcstombs@@$$J0YAIPADPBGI@Z)
In my project:
Runtime Library is: Multi-threaded Debug (/MTd)
Ignore All Default Libraries is: No
I’m not sure why the CRT is not linking into the project. If anyone has any
suggestions on how to fix this or better yet, a managed way to convert my
String* to a char*, that would be great!
Thanks for any help you can provide.