debugging mixed mode assembly

L

Lonewolf

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.
 
W

Willy Denoyette [MVP]

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 said:
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.
 
G

Guest

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.
 
L

Lonewolf

Willy said:
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.
 
W

Willy Denoyette [MVP]

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.


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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top