473,399 Members | 2,146 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,399 software developers and data experts.

debugging mixed mode assembly

hi,
I'm still in the process of transiting from MFC/VC6 to vs2005, and a
lot of things are very alien to me. So hope you could bear with me if my
question sounds stupid. Basically I have native codes written in VC6
which I want to encapsulate in a managed assembly using C++/CLI so that
I can use it in C# easily without all the interop codes. So, my question
is, what is the replacement for MFC's TRACE macro in VS2005's C++/CLI
in native(unmanaged) codes?

I tried to see what's inside a WCHAR buffer declared as

WCHAR Buffer[256];

in MFC, it's as simple as
TRACE1("\n Buffer=%s", Buffer);

I tried using
Debug::Write("\n Buffer="+Buffer);
which failed spectacularly.

I also tried
String^ s;
s->format("\n Buffer=%s", Buffer);
Debug::Write(s);
which the IDE complained as parameter 2 of String::format is not Object,
which obviously is not since this is in native code. I also tried
ATLTRACE2(atlGeneral, 4, "\n Buffer=%s", Buffer);
but I was unable to see anything in the IDE's output window.,

when I used win32's OutputDebugString directly, coupled with wcsprintf,
I was able to achieve something like the TRACE macro. However, I would
expect VS2005 to free me from this hassle. So, could someone please
enlighten me on this? thank yoy very much.
Jan 5 '06 #1
4 2210
Again why do you call this unmanaged code?
C++/CLI compiles to managed code,
String^ s; is a managed string reference.
this:
s->format("\n Buffer=%s", Buffer);
can't even compile, format is not a String method!

so what you should do is this (a copy of a reply to your other posting):

<
I guess you mean from managed code, here is how...

wchar_t *unm = L"Test";
String ^s = String::Format("{0}", Marshal::PtrToStringUni((IntPtr)unm));
Debug::Write(s);
But you should definitely read the MSDN docs about unmanaged/managed interop
and managed debugging before posting.

Willy.


"Lonewolf" <an*******@mozilla.org> wrote in message
news:OY**************@TK2MSFTNGP11.phx.gbl... hi,
I'm still in the process of transiting from MFC/VC6 to vs2005, and a lot
of things are very alien to me. So hope you could bear with me if my
question sounds stupid. Basically I have native codes written in VC6 which
I want to encapsulate in a managed assembly using C++/CLI so that I can
use it in C# easily without all the interop codes. So, my question is,
what is the replacement for MFC's TRACE macro in VS2005's C++/CLI in
native(unmanaged) codes?

I tried to see what's inside a WCHAR buffer declared as

WCHAR Buffer[256];

in MFC, it's as simple as
TRACE1("\n Buffer=%s", Buffer);

I tried using
Debug::Write("\n Buffer="+Buffer);
which failed spectacularly.

I also tried
String^ s;
s->format("\n Buffer=%s", Buffer);
Debug::Write(s);
which the IDE complained as parameter 2 of String::format is not Object,
which obviously is not since this is in native code. I also tried
ATLTRACE2(atlGeneral, 4, "\n Buffer=%s", Buffer);
but I was unable to see anything in the IDE's output window.,

when I used win32's OutputDebugString directly, coupled with wcsprintf, I
was able to achieve something like the TRACE macro. However, I would
expect VS2005 to free me from this hassle. So, could someone please
enlighten me on this? thank yoy very much.

Jan 5 '06 #2
check out the Trace class of the system diagnostics namespace.
for example:

if you have a WCHAR string, you can simply create a managed string by
passing that WCHAR string to a managed string constructor. then you can pass
that managed string to Trace.

Trace::WriteLine(L"Hello World");
will print Hello world as tracing output.
the neat thing is that you can enable or disable trace statements at runtime.

with TraceListeners you can even create your own handling of tracing output
(for example logging to disk etc).

kind regards,
Bruno.

"Lonewolf" wrote:
hi,
I'm still in the process of transiting from MFC/VC6 to vs2005, and a
lot of things are very alien to me. So hope you could bear with me if my
question sounds stupid. Basically I have native codes written in VC6
which I want to encapsulate in a managed assembly using C++/CLI so that
I can use it in C# easily without all the interop codes. So, my question
is, what is the replacement for MFC's TRACE macro in VS2005's C++/CLI
in native(unmanaged) codes?

I tried to see what's inside a WCHAR buffer declared as

WCHAR Buffer[256];

in MFC, it's as simple as
TRACE1("\n Buffer=%s", Buffer);

I tried using
Debug::Write("\n Buffer="+Buffer);
which failed spectacularly.

I also tried
String^ s;
s->format("\n Buffer=%s", Buffer);
Debug::Write(s);
which the IDE complained as parameter 2 of String::format is not Object,
which obviously is not since this is in native code. I also tried
ATLTRACE2(atlGeneral, 4, "\n Buffer=%s", Buffer);
but I was unable to see anything in the IDE's output window.,

when I used win32's OutputDebugString directly, coupled with wcsprintf,
I was able to achieve something like the TRACE macro. However, I would
expect VS2005 to free me from this hassle. So, could someone please
enlighten me on this? thank yoy very much.

Jan 5 '06 #3
Willy Denoyette [MVP] wrote:
Again why do you call this unmanaged code?
C++/CLI compiles to managed code,
String^ s; is a managed string reference.
this:
s->format("\n Buffer=%s", Buffer);
can't even compile, format is not a String method!

so what you should do is this (a copy of a reply to your other posting):

<
I guess you mean from managed code, here is how...

wchar_t *unm = L"Test";
String ^s = String::Format("{0}", Marshal::PtrToStringUni((IntPtr)unm));
Debug::Write(s);

OK, I understand now. But still, is there no C++/CLI substitute for the
MFC TRACE or the C "printf" which can be used conveniently? it's such a
bloody pain to write all the marshal code. right now, I'm using back the
old dprintf function which I used in the old days of Windows SDK
programming. it's declared as,

void cdecl dprintf(WCHAR* szFormat, ...)
{

static WCHAR Buffer[1024];
wvsprintf( Buffer, szFormat, (LPSTR)(&szFormat+1) );
OutputDebugString(Buffer);
}
this fucntion behaves like printf, but outputs to the debug window. But
it will not be like MFC TRACE which expands to nothing in dbeug build.
so I have to put #ifdef_DEBUG everywhere I use it. Using this method, I
would be able to do

dprintf("this is string1, %s and this is string2, %s", pszStr1, pszStr2);

using the Debug::Write and String, I have to form 2 string and
concartenate them in Debug::Write, a real pain. Didn't MS provide
anything simpler adn easier to use?

My apologies for asking if it sounds really dumb.
Jan 6 '06 #4
Your problem stems from the fact that you are mixing managed types (String^)
and unmanaged types (wchar_t etc...) in one compilation unit (that's called
mixed mode). If you want to use the managed System::Diagnostics::Debug class
methods methods you need to pass them pure managed types, else you have to
marshal the unmanaged data to managed data. The same is true for printf, if
you need to pass a managed String to printf you have to marshal the String
to a native type (char*, wchar_t*), this is the price you pay when using
mixed mode in C++/CLI. If you aren't prepared for this, you'll have to stick
with or pure unmaged code (using MFC, ATL stuff) or use pure managed code.
In the latter case you should start to read (and use) the framework class
docs, these class libraries are key in .NET development, they are actually
more important than ATL or MFC ever was for unmanaged code.

Anyway, a simple function like this:

void DebugOut(whar_t *buff)
{
String ^s = String::Format("{0}",
Marshal::PtrToStringUni((IntPtr)buff));
Debug::Write(s);
}

is all you need to output debug mesages in mixed mode programs. If this is
not enough, no-one stops you from using ATL in mixed mode C++/CLI programs.

Willy.
"Lonewolf" <an*******@mozilla.org> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
| Willy Denoyette [MVP] wrote:
| > Again why do you call this unmanaged code?
| > C++/CLI compiles to managed code,
| > String^ s; is a managed string reference.
| > this:
| > s->format("\n Buffer=%s", Buffer);
| > can't even compile, format is not a String method!
| >
| > so what you should do is this (a copy of a reply to your other posting):
| >
| > <
| > I guess you mean from managed code, here is how...
| >
| > wchar_t *unm = L"Test";
| > String ^s = String::Format("{0}",
Marshal::PtrToStringUni((IntPtr)unm));
| > Debug::Write(s);
|
|
| OK, I understand now. But still, is there no C++/CLI substitute for the
| MFC TRACE or the C "printf" which can be used conveniently? it's such a
| bloody pain to write all the marshal code. right now, I'm using back the
| old dprintf function which I used in the old days of Windows SDK
| programming. it's declared as,
|
| void cdecl dprintf(WCHAR* szFormat, ...)
| {
|
| static WCHAR Buffer[1024];
| wvsprintf( Buffer, szFormat, (LPSTR)(&szFormat+1) );
| OutputDebugString(Buffer);
| }
| this fucntion behaves like printf, but outputs to the debug window. But
| it will not be like MFC TRACE which expands to nothing in dbeug build.
| so I have to put #ifdef_DEBUG everywhere I use it. Using this method, I
| would be able to do
|
| dprintf("this is string1, %s and this is string2, %s", pszStr1, pszStr2);
|
| using the Debug::Write and String, I have to form 2 string and
| concartenate them in Debug::Write, a real pain. Didn't MS provide
| anything simpler adn easier to use?
|
| My apologies for asking if it sounds really dumb.
Jan 6 '06 #5

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

Similar topics

9
by: Edward Diener | last post by:
I received no answers about this the first time I posted, so I will try again. My inability to decipher an MSDN topic may find others who have the same inability and someone who can decipher and...
8
by: Bern McCarty | last post by:
Is it at all possible to leverage mixed-mode assemblies from AppDomains other than the default AppDomain? Is there any means at all of doing this? Mixed-mode is incredibly convenient, but if I...
8
by: Nadav | last post by:
Hi, I am writing a performence critical application, this require me to stick to unmanaged C++ as performance is much better using unmanaged C++ ( about 33% better ), Still, I am trying to avoid...
1
by: Kluch | last post by:
Hello, I have a mixed mode DLL that I am trying to debug that's acting really weird, Basiclly I expose a bunch of functions through virtual functions and other code loads it that way. What's...
2
by: Ben Harper | last post by:
I'm trying to load an assembly of mine from a C# webforms page, but IIS fails to load the page because of failed dependencies. My problem is that I cannot for the life of me discover the dependency...
7
by: Kevin Frey | last post by:
Using .NET 1.1. We have a mixed-mode assembly written in Managed C++ that we are using from an ASP.NET application that has been coded using C#. The mixed-mode assembly has its own...
8
by: Edward Diener | last post by:
By reuse, I mean a function in an assembly which is called in another assembly. By a mixed-mode function I mean a function whose signature has one or more CLR types and one or more non-CLR...
0
by: Russ Barrett | last post by:
Hello, I'm new to ASP.Net and I'm trying to interface an ASP.Net (C#) application to functionality resident in a C++ static lib. From posts I've seen around I understand this requires creation of a...
5
by: phnimx | last post by:
Hi , We have developed a number of plug-in .NET Library Components that we typically deploy with our various applications by installing them into the GAC. Each of the applications contains an...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...

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.