ArgumentException: The pointer passed in as a String must not be in the bottom 64K of the process's

  • Thread starter Thread starter Bob
  • Start date Start date
B

Bob

I have a managed C++ class that uses an unmanaged C++ library. That library
stores some character strings which I access through a char*. I use that
string as an argument to a String::IndexOf(). When I run, I sometimes get
either of the following exceptions:

"System.ArgumentException: The pointer passed in as a String must not be in
the bottom 64K of the process's address space."
"System.ArgumentOutOfRangeException: Pointer startIndex and length do not
refer to a valid string."

When I look at the string in the debugger, it is garbage. (e.g.
"ô<hT.,½/"). The string was set in the unmanaged library with the
following code:

System::IntPtr imagePointer =
(System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(imageName));
char* charPointer = (char*)imagePointer.ToPointer();
unmanagedLib->SetString(charPointer);
System::Runtime::InteropServices::Marshal::FreeHGlobal(imagePointer);

Why is this? What am I missing that I need to do to use these strings? Do
I need to also marshal the unmanaged string somehow?

Thanks! Bob
 
Why is this? What am I missing that I need to do to use these strings? Do
I need to also marshal the unmanaged string somehow?

The pointer is invalid after you call FreeHGlobal.



Mattias
 
Let me clearify...I have tried this 2 ways. I don't know if the unmanaged
library keeps the charPoint that was passed to the SetString method or it
copies it to it's own buffer. I also tried not calling FreeHGlobal but that
does not seem to make a difference. I still end up with garbage in the
string returned from the unmanaged library. I'm wondering if it has
something to do with how that library may be storing it.
 
Here is the code for the unmanaged library:

virtual void fileName (const char* fileName);
inline const char* fileName(void) const;

void Class::fileName(const char* fileName)
{
delete [] _fileName;
_fileName = NULL;
if (fileName != NULL)
{
_fileName = new char[strlen (fileName) + 1];
strcpy (_fileName, fileName);
}
}

const char* Class::fileName(void) const
{
return _fileName;
}
 
Hi Bob,

Based on my understanding, now the problem is: You are developing a managed
C++ application and an unmanaged C++ library. That C++ library stores some
character strings which is accessed through a char*. When you run managed
client, you got error "System.ArgumentException: The pointer passed in as a
String must not be in the bottom 64K of the process's address space."
Please feel free to post here if I have misunderstood the problem.

In order to troubleshoot the problem, could you please post here the sample
code of that unmanaged C++ library and managed C++ client? We will look
into it, reproduce it on our side and do our best to find out the reason of
it. A small repro sample is OK.

If you have any more concerns, please feel free to post here.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
This problem only happens occassionally and I have not been able to
reproduce it in a small example. It has to part of the unmanaged library.
This is a third party commercial product that we do not control.

What I really need is an understanding as to what the exception "The pointer
passed in as a String must not be in the bottom 64K of the process's address
space" really means. I did a search on the web and your web site and could
find no additional information. Why would that new/strcpy in the unmanaged
library (_fileName = new char[strlen (fileName) + 1];) put data in the
"bottom 64K" and what exactly does that mean? Any help you can provide
would be appreaciated!
 
You probably need to marshal the unmanaged char * back to a System.String.

Have a look at Marshal.PtrToStringAnsi.

If you were intending to alter the managed string's internal buffer you
can't, because it's basically a bstr, and you shouldn't, because managed
strings are immutable.
 
Hello Bob,

I reviewed this issue carefully, and think more information is needed
before moving forward:

1. Could you please tell me the content of call stack when the error
occurs? That will help us determine which function calls cause the problem.

2. >> "The pointer passed in as a String must not be in the bottom 64K of
the process's address space"
Based on my exerience, I believe this error occurs when a string is
corrupt. For example, it somehow write exceeding the string boundary.

3. In the Class::fileName, I suggest you to check if _fileName equals to
NULL before deleting it.

//--------------code---------------
void Class::fileName(const char* fileName)
{
delete [] _fileName;
//-----------------end of-----------

I look forward to your response.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top