Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / March 2005

Tip: Looking for answers? Try searching our database.

Problem with C++ string marshalling in .NET 2.0

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Travis - 17 Mar 2005 06:11 GMT
I'm working on a project where we're building a .NET wrapper to IBM's MQ
Workflow C++ API.

My original intention was to wrap the Workflow API with a managed C++
assembly that in turn could be consumed by .NET clients.  I've developed a
proof of concept  app in .NET 1.1 that validated this.

However, the final deliverables need to be in .NET 2.0 so I migrated the 1.1
code to 2.0.  After the migration effort, I tried running the app and
immediately received an AccessViolationException - "Attempted to read or
write protected memory.  This is often an indication that other memory has
been corrupted".

The code that worked fine in 1.1 doesn't want to play in 2.0 - (snippet below)

//Note: username and password are managed Strings
const char * uUsername = (const
char*)(Marshal::StringToHGlobalAnsi(username)).ToPointer();

const char * uPassword = (const
char*)(Marshal::StringToHGlobalAnsi(password)).ToPointer();

rc = _service->Logon(uUsername, uPassword, FmcjService::Default );

Marshal::FreeHGlobal(System::IntPtr((void*)uUsername));

Marshal::FreeHGlobal(System::IntPtr((void*)uPassword));

The exception is thrown on when the logon is attempted.  The assembly is
being compiled using the /clr switch.

Here's the compiler command-line:

/Od /I "C:\Program Files\IBM WebSphere MQ Workflow\API" /AI
"C:\Source\TPoC\Debug" /D "WIN32" /D "_DEBUG" /D "_WINDLL" /D "_MBCS" /FD
/EHa /MDd /GS /Yu"stdafx.h" /Fp"Debug/WFWrapper.pch" /Fo"Debug/"
/Fd"Debug/vc80.pdb" /W3 /nologo /c /Zi /clr /TP /FU
"c:\WINDOWS\Microsoft.NET\Framework\v2.0.40607\mscorlib.dll" /FU
"c:\WINDOWS\Microsoft.NET\Framework\v2.0.40607\System.dll" /FU
"c:\WINDOWS\Microsoft.NET\Framework\v2.0.40607\System.Data.dll" /FU
"c:\WINDOWS\Microsoft.NET\Framework\v2.0.40607\System.Drawing.dll" /FU
"c:\WINDOWS\Microsoft.NET\Framework\v2.0.40607\System.Windows.Forms.dll" /FU
"c:\WINDOWS\Microsoft.NET\Framework\v2.0.40607\System.XML.dll"

....and the linker command-line

/OUT:"C:\Source\TPoC\Debug\WFWrapper.dll" /INCREMENTAL /NOLOGO /DLL
/MANIFEST
/MANIFESTFILE:"c:\Source\TPoC\debug\WFWrapper.dll.intermediate.manifest"
/INCLUDE:"__DllMainCRTStartup@12" /DEBUG /ASSEMBLYDEBUG
/PDB:"C:\Source\TPoC\Debug/WFWrapper.pdb" /FIXED:No mscoree.lib "C:\Program
Files\IBM WebSphere MQ Workflow\API\*.lib" msvcrtd.lib  kernel32.lib
user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib
ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib "..\..\Program
Files\Microsoft Visual Studio 8\SDK\v2.0\Lib\mscoree.lib"  libsmanifest.res

Has anyone else experienced similar problems in .NET 2.0?  Any help in
solving this will be greatly appreciated.

Cheers,

Travis
William DePalo [MVP VC++] - 17 Mar 2005 06:48 GMT
> I'm working on a project where we're building a .NET wrapper to IBM's MQ
> Workflow C++ API.
[quoted text clipped - 10 lines]
>
> rc = _service->Logon(uUsername, uPassword, FmcjService::Default );

I should tell you that I haven't done any .Net 2.0 development so this is a
bit of guesswork. When I looked up the string marshaller in the
documentation I found this about the return value:

<quote>
The address, in unmanaged memory, to where s was copied, or 0 if a null
reference (Nothing in Visual Basic) string was supplied.
</quote>

You may want to remove the calls to ToPointer() .

If that doesn't work, please reply here so that someone else may offer
better advice.

Regards,
Will
Kapil Khosla [MSFT] - 17 Mar 2005 17:28 GMT
Signature

This posting is provided "AS IS" with no warranties, and confers no
rights."Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm"

>> I'm working on a project where we're building a .NET wrapper to IBM's MQ
>> Workflow C++ API.
[quoted text clipped - 21 lines]
>
> You may want to remove the calls to ToPointer() .

I dont see anything wrong is using ToPointer() to convert a managed string
to a native string. The issue seems to be in rc = _service->Logon(uUsername,
uPassword, FmcjService::Default );
Could you share out more information on the type rc and _service, without
that it is very hard to understand what is going on.
Thanks.
Kapil

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.