Debug C#.Net DLL Project from VB6 DLL project - c#

I have three projects,
One is a VB6 project which compiles to an EXE.
Another is also a VB6 project, which compile to a DLL.
And the last one is a .Net (2010) project which compiles to DLL.
The DLL from .Net is referenced in my VB6 DLL project.
And the resulting VB6 DLL project is reference in my VB6 EXE project.
In this scenario, how can I debug my .Net DLL project?
It is failing (or not able to debug) in both cases, even if I attach the (VB6 DLL) process to my .Net DLL Project, or I setup the ‘start external program’ to my VB6 DLL project on .Net DLL Project.
Do I have to change the reference (.Net DLL) on my VB6 DLL Project to get it in debug?
All your valid inputs are welcome.
(Please explain the procedures step by step which I need to do)
Thanks in advance.

You should be able to debug the C#.NET dll by setting the start action under Debug to
Start external program
and specify the VB6 executable as the program.
I know this works as I use this to debug a .NET/C# plugin for an old VB6 application.
It shouldn't matter how many other dlls (.NET or otherwise) you have to go through to get to the code you are trying to debug.

ah vb6 :) it's been a long time....
basically, you can't debug .NET code within VB6 IDE.
However, nothing stops you from creating a .NET test project to unit test the .NET dll. And that's what you should have done prior to reference it in VB6.
If you need to track down a specific issue, another way you can use is to write debugging infos to a file/database/event view/... when the dll is in debug mode, like which functions were called, parameters, stack trace...

This might not be correct, but I believe VB 2010 uses what? .NET 4.0, right? VB6 was created in like...1994 I believe, so it would be <= .NET 1.0. The binaries would more than likely be incompatible.

Related

Why does a reference to a C/C++ DLL project from a C# class library project break in Visual Studio when building the solution?

I am in the process of creating a library to be used in Windows desktop applications. The core part of the library will be a C/C++ DLL that can be used in Win32 applications or other C/C++-based apps. I'll just call it libtest.dll for easy referencing here. I also want to make it available to Windows Forms applications (C#), so I am creating a C# class library (DLL) as a wrapper around the C/C++ library using P/invoke (DllImport) to call into the C/C++ code. I'll refer to this library as Test.dll.
Additionally, I have a Windows Forms project set up in Visual Studio for testing that depends on the C# class library project. The C# class library depends on the C/C++ library project. However, for Test.dll to find libtest.dll (in the DllImport statements), libtest.dll needs to either be in the same directory as Test.dll or in one of the DLL search paths in the system.
To make this easy when debugging in Visual Studio, I thought adding the libtest.dll project as a reference to the Test.dll project would automatically copy libtest.dll into the output directory of the Test.dll project. When I add the reference and then build the solution, the reference ends up broken and the libtest.dll file is not copied into the output directory of Test.dll.
When I add the reference to the Test.dll project in Visual Studio, the properties of the reference look fine:
"Copy Local" is set to True
The path is correct
"Resolved" is True
After building, however, the reference itself has a warning icon on it, and the path is now empty and "Resolved" is False. Any idea why, or how to fix this? How do I set up the project reference between these two library projects so the C/C++ DLL gets copied into the output directory of the C# class library project? I have verified that the build order of the two projects is correct: first libtest then Test.

Native C++ dll in one solution with C# project

I have a source file written in C++, performing some simulation. I would like to create a user interface for this in C#. How can I have both C++ and C# in a single Visual Studio solution?
What I did so far was I created a .NET C# project and solution, and then a native project as a DLL library. I have marked the C# project as "Depends on" the C++ dll. I have provided the necessary dllexport directives and imported using DllImport. Everything compiles fine, but when I execute the application, the dll is not found:
Unable to load DLL 'xxxx.dll': The specified module could not be found.
It is no surprise the DLL is not found, as the application is run from its output directory and the output directories are different for the C# and C++ projects. I do not want to write an explicit path as my import like `"..\..\Debug\xxxxx.dll".
Is there some common way how to structure a solution containing native a Dll and C# app so that the C# app can call the native Dll?
If you know that after deployment your C++ DLL will be in the same folder as your C# DLL (or executable), then on simple way to solve the problem is to use the PostBuild event (Project properties, Build events).
You can add a copy command that will put your C++ DLL into the C# output folder.
I found a very comfortable way, which might have some unexpected drawbacks, but I do not see any so far: I can enable CLR for the C++ DLL project, making it "mixed", even if it in fact does not contain any managed code, and then the project building it can be made a Reference in the C# .NET project.
To make the project mixed mode set Configuration Properties / General / Common Language Runtime Support to Common Language Runtime Support (/clr).
After this the build system copies the DLL into the application output folder automatically.

VB6 to COM-callable wrapped .NET - problems finding .NET libraries

Premise:
Wrote a COM-callable wrapper (CCW) written in C# to be called by VB6 code.
The C# code has .NET libraries (third party) that it must utilize.
The wrapped C# class instantiates fine, raises events properly, takes method calls properly.
Problem:
The VB6 code, when running the C# code, gets an error when the C# code attempts to access the additional .NET libraries mentioned above.
Process tested:
C# wrapped code completed.
VB6 code written, references the COM dll created.
"File not found..." error received when the C# code tries to access the .NET libraries from inside itself.
Copied the third party .NET libraries into the main folder of the VB6 code (also into system32 folder).
Still "File not found..." error.
Wrote a C# Windows Form test project. Referenced the C# wrapped code.
Received the same error as the VB6 code.
In C# Windows Form test project, referenced the .NET libraries used by the C# wrapped code.
The program ran just fine.
Conclusion/Question:
Can VB6 call/use a com-callable wrapped C# program that references other third party .NET libraries?
I've done this with tons of third party libraries and as long as the third party library DLL's are in the same directory as your C# DLL's there shouldn't be a problem.
Copying them into the same directory as your VB6 code will do nothing, the effective directory of VB6 code when running the in VB6 debugger is %ProgramFiles%\Microsoft Visual Studio\VB98, so the code running will have no visibility of that.
The system32 directory has nothing to do with DotNet dll's (the only DLL's they would affect is PInvoke'd dll's and you say you add a reference so that isn't the case).
Adding the third party library to the GAC should fix things, however that is not necessarily possible/easy due to the need to sign the DLL. So just make sure the Libraries you are referencing are in the directory of YOUR library and that VB6 is actually referencing that version of your library (Debug/Release whatever what last compiled). You may want to hand RegAsm the library to make sure the correct library is Com Registered.
So to sumerize, suppose your source code looks like this:
c:\projects\vb6project
c:\projects\c#project
And your project is set to Debug, the third party libraries should be in:
c:\projects\c#project\bin\Debug
When you actually want to distribute the application, make sure that ALL the libraries are in the same directory as your VB6 exe.
This will mirror somewhat the answer by Kris Erickson.
First, for my usage which solves these problems, I did not use the GAC. Assuming that my VB6 application is in c:\program files\mycompany\vb6app.exe, I placed the COM-friendly .NET dll (lets call it net4vb.dll) in the folder with the executable. I then regsiter that dll there with RegAsm using the /codebase option. Finally, I place the .NET assembly that I'm wrapping or using (lets call it purenet.dll) in that folder as well.
At this point, vb6app.exe should run with no errors.
It is important to note that if you select the "Register for COM Interop" in Visual Studio then it will overwrite the registration you did above. So do not use this option. Stick with the command-line tools only for this.

What are general steps for me to port my project to mono?

I have a dll. and visual C++ source for it. (my dll is visual c++ wraper around some ffmpeg av* libs - another precompiled dll's) while in the same solution (.sln) I have C# project that uses my dll.
what are general steps for me to port my project to mono?
Mono does not support mixed-mode assemblies on non-Windows operating systems.
Remove your C++ wrapper and rewrite your application to only use P/Invoke to call into native code.
See also: www.mono-project.com/CPlusPlus
Compile your project in Visual Studio, but enable the following compiler options:
/clr:pure: This will make a pure CLR assembly (without any x86/x86-64 asm), that should run on mono, unless you use some C runtime calls (CRT).
/clr:safe: The same as pure, but you can't have CRT references, so your project might not compile. If it doesn't, replace those function calls with other portable calls.
The resulting assembly should be pure CLR and will work with Mono.
This page contains everything you might encounter when compiling a version that works on Mono.
I would suggest also take a look at this -
http://mono-tools.com/store/
There are various tools available to help you port to Mono - you can even debug within visual studio and package it for deployment on Linux.

use vc++ class library in c#

i have created a simple "public ref class" in the vc++ project, which is configured to generate a dynamik library.
In the c# Project (simple console application) i added the vc++ project as to the References and doing a "using myVC++library".
But when i try to create an object from the vc++ dll i always get: System.BadImageFormatException was unhandled
any clues or helpfull tutorials on this?
TIA
Is your c# project set to build for "Any CPU" - if it is, and you're on a 64-bit OS, then the load will fail.
The VC++ dll will be 32-bit only, and 64-bit app can't run it.
You could change your c# build to target x86 instead.

Categories

Resources