How to debug DLL?  
Author Message
aronsatie





PostPosted: Visual C# Express Edition, How to debug DLL? Top

Hi,

I have a dll written in C++ (6.0 MFC). The functions of this dll is called by an application I just started in C# 2005 Express edition. I read about how to call a dll in C#, but my problem is the reverse situation.

I need to debug the dll to find out if all parameters are passed correctly from C# to the dll functions. But if I set the exe file compiled from the C# to test the dll, it does not even start (it does when I start outside of the C++ de****).

What is wrong and what can I do

I would be grateful for any help.

Peter



Visual Studio Express Editions13  
 
 
Mike Danes





PostPosted: Visual C# Express Edition, How to debug DLL? Top

"But if I set the exe file compiled from the C# to test the dll, it does not even start (it does when I start outside of the C++ de****)."

If I understand correctly you are trying to debug the exe built by C# Express edition using VC++ 6.0. The problem is that VC++ 6.0 does not know nothing about .NET applications and even if the app would start you will have some trouble debugging it.

The best thing that comes to my mind now is the following:

1. Build the dll in VC++ 6.0

2. Set breakpoints in the begining of the dll functions that you need to debug

3. Start the application (from outside of the VC++ 6.0)

4. Attach the de**** to the application (you should be able to do this from Task Manager, select your application process, right click and select debug)

Unfortunately this step may fail because the de**** installed by VC# Express may replace the de**** settings of VC++ 6.0 and I'm not sure what you can do about it... maybe use VC++ 2005 Express as a C++ de**** instead of VC++ 6.0 but this is getting complicated

5. Do something in your application that would trigger calls to functions from the dll (like pressing a button or something, depending on what your application does). You may need to use a Thread.Sleep or other form of delay (waiting for a keypress) perhaps in your application so you can have enough time to attach the de****).


 
 
aronsatie





PostPosted: Visual C# Express Edition, How to debug DLL? Top

Hi Mike,

Thanks for the answer. To your points:

1. Yes, I did that.

2. Yes, I did that.

3. Hmm. Strange. Why can't I set the executable in VC++6.0 using Project - > Settings -> Debug to the executable compiled by C# 2005 Do you know why this does not work In fact, I also tried VS.net 2003 (I made the same C++ dll project in .net 2003), tried to set up Project Properties to use this exe to debug and it fails too.

4. I was offered VS.net 2003 de**** to attach to my exe, then I had to choose from a list (CLR, Direct3D, Native code). Which one should I choose I ask this because I was not successful.

5. I could modify the code like this, but could you explain why is this necessary at all

Please keep in mind that I want to debug my C++ dll, not the C# executable, although the purpose of debugging is checking if the arguments passed to the functions of the dll 'arrive' in correct form. Of course, I would also need to work on the dll in the future, and somehow I need to debug, and I need the executable for that as well.

Once again, your help would be much appreciated, I am completely stuck right now.

Thanks,

Peter


 
 
Mike Danes





PostPosted: Visual C# Express Edition, How to debug DLL? Top

Unfortunately I don't have a VC++6.0 to play with but I suspect it cannot start the C# executable because that's a .NET executable... it does not have native code in it... VC++ 6.0 would not even be able to set a breakpoint in that executable. If you could start the executable manually and then attach VC++ 6.0 de**** to it maybe it will work but as you saw you are offered only VS2003.

I don't remember now but doesn't VC++6.0 have  an Attach command in the Debug menu If it has try that one. If not you can try attaching it using the command line:

msdev -p 0x1234

(replace 0x1234 with the hexadecimal PID of the process you want to attach to).

 

For VS2003 try choosing Native Code (CLR won't work because VS2003 uses .NET v1.1 while Visual C# 2005 uses .NET v2.0 and of course, Direct 3D does not apply).

 

I understand that you want to debug the C++ dll and not the C# executable but the problem is that older de****s have trouble starting that executable.

 


 
 
aronsatie





PostPosted: Visual C# Express Edition, How to debug DLL? Top

I tried VS2003 and chose Native Code. This is what I got:

(Disassembly cannot be displayed in run mode.)

I more or less gave up on VS 6.0, thought I maybe have a better chance with VS2003. Now I understand that an executable is totally different compiled with VS2005 and it is not native code.

Do you think I would be better off redoing the dll in VC++ 2005 as an unmanaged dll I would like to keep this two-part setup in the application. There are highly optimized parts and also DirectShow stuff in the dll that I can't do in managed C# but on the other hand making the UI and handle database stuff would be a real pain in unmanaged C++.

At the moment I have the express edition of C# and C++ as I try to evaluate if I should upgrade. Would it make a difference in my situation if I had one of the 'full' versions With C++ Express it seems I cannot even make a DLL, so I do not even know if I can rewrite my dll with it.

Please advise, I really appreciate your help.

Best regards,

Peter


 
 
Mike Danes





PostPosted: Visual C# Express Edition, How to debug DLL? Top

If you try to debug with VS2003 you should also try to build the C++ dll with it. Also you will need to have the C++ project loaded in VS2003 (or at least the cpp files where you want to set breakpoints) before attaching.

As for not being able to create a dll with VC++ Express you can try opening the vcproj file create by VS2003 with VC++ Express. I bet it will upgraded it. Also you can create a static lib and then manually change the propject properties to create a dll. However the problem with VC++ Express is that it does not have MFC and ATL so if use those 2 libraries you cannot use VC++ Express. Also VC++ Express does not come with Platform SDK so you will need to download that and install it.

Of course having a 'full' version of VS2005 would help. You can have both the C++ dll and the C# exe in the same solution, set breakpoints in any of them, press F5 and debug both of them. Less, less trouble...


 
 
aronsatie





PostPosted: Visual C# Express Edition, How to debug DLL? Top

Hi Mike,

Finally I succeeded with attaching the 2003 de**** to the C# 2005 exe. It is not a perfect solution, and I will try to recode the dll in C++ 2005 to see if it is easier to use.

If you don't mind I will post here the 'final result'.

Thanks very much for your help!

Regards,

Peter