473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Problem with C++ string marshalling in .NET 2.0

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)).ToP ointer();

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

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

Marshal::FreeHGlobal(System::IntPtr((void*)uUserna me));

Marshal::FreeHGlobal(System::IntPtr((void*)uPasswo rd));

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\msc orlib.dll" /FU
"c:\WINDOWS\Microsoft.NET\Framework\v2.0.40607\Sys tem.dll" /FU
"c:\WINDOWS\Microsoft.NET\Framework\v2.0.40607\Sys tem.Data.dll" /FU
"c:\WINDOWS\Microsoft.NET\Framework\v2.0.40607\Sys tem.Drawing.dll" /FU
"c:\WINDOWS\Microsoft.NET\Framework\v2.0.40607\Sys tem.Windows.Forms.dll" /FU
"c:\WINDOWS\Microsoft.NET\Framework\v2.0.40607\Sys tem.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.i ntermediate.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

Nov 17 '05 #1
2 1981
"Travis" <Tr****@discussions.microsoft.com> wrote in message
news:27**********************************@microsof t.com...
I'm working on a project where we're building a .NET wrapper to IBM's MQ
Workflow C++ API.
...
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)).ToP ointer();

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

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

Nov 17 '05 #2


--
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"

"William DePalo [MVP VC++]" <wi***********@mvps.org> wrote in message
news:ej**************@tk2msftngp13.phx.gbl...
"Travis" <Tr****@discussions.microsoft.com> wrote in message
news:27**********************************@microsof t.com...
I'm working on a project where we're building a .NET wrapper to IBM's MQ
Workflow C++ API.
...
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)).ToP ointer();

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

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() .

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
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: PHil Coveney | last post by:
Hello, I am having difficulty marshalling structures when calling DeviceIoControl. I am importing this Win32 function as static extern int DeviceIoControl (int hDevice, int...
0
by: Shashank Date | last post by:
/* C# Gurus, I am trying to use interop marshalling to call SetCommTimeouts win32 API. But I keep getting the "Object reference not set to an instance of an object" error. Can anybody help...
6
by: Chris | last post by:
Hi, how is a String implemented in .NET : as in VB.NET : using a length-prefix located in front of the actual characters or as in C : using '\0' (backslash null) thnx
1
by: John Ye | last post by:
Hello Folks! I have a vendor that has supplied me with a VB6 Module and DLL (and it works in VB6) that I would really like to migrate to VB.Net. I have used the Upgrade Wizard in VS.Net but it...
4
by: Scott Lemen | last post by:
Hi, Some Win APIs expect a structure with a fixed length string. How is it defined in VB .Net 2003? When I try to use the FixedLengthString class I get an "Array bounds cannot appear in type...
9
by: mupe | last post by:
Hi, i have a problem with a Type Library, which is written in C++. I am developing an application in C#.NET and have to use functions from this COM-Type Library. When I use these functions in...
1
by: gudiya | last post by:
hello everybody I am having a very wierd problem.i am developing a win ce applicationin which I am marshalling win ap function findfirstfile and findnextfile in c# .My problem is it reads the file...
2
by: d-42 | last post by:
Hi, I'm pretty sure I've just got a Marshalling problem, but I'm completely stumped. If there is a better newsgroup to post this in, please point me towards it. First I'm trying to use...
1
by: d-42 | last post by:
Hi, I'm pretty sure I've just got a Marshalling problem, but I'm completely stumped. If there is a better newsgroup to post this in, please point me towards it. First I'm trying to use...
1
by: JohnCox | last post by:
I have a simple Win32 DLL I wrote named "SimpleLib" that exports two functions. It is written in C++ and compiled with __stdcall (/Gz) and with the preprocessor definition _MBCS (not Unicode). ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.