Loading DLLs with Microsoft Visual C# 2010 Express - c#

I'm using Microsoft C# 2010 Express with the FrameLink Express drivers from Imperx. When I try to load the DLL at runtime, I get an exception:
Unable to load DLL 'VCECLB.dll': The specified module could not be
found. (Exception from HRESULT:0x8007007E)
If I try to add the DLL as a reference, I get this message:
A reference to 'C:\\VCECLB.dll' could not be added. Please
make sure that the file is accessible, and that it is a valid assembly
or COM component.
Two questions:
Is this an Express problem (i.e. some limitation with Express causing the problem)? I believe the source for the DLL is written in C++, but I wouldn't think that would matter.
Is there a different way to reference the DLL and/or add it to the project? Something I'm doing wrong?
Finally, I've tried adding both the x86 and x64 versions, debug and release. Same failure to add as a reference every time.

That DLL doesn't seem to be .NET (nor COM) code - therefore you cannot load it, nor add a reference to it.
Doesn't have anything to do with your Express version - it's just not a .NET "compatible" DLL and thus you can't load or reference it.
In order to use its functionality, you'll probably have to dive into more "arcane" (and more elaborate and more complicated) methods of calling raw C++ code from .NET - or find a .NET compatible counterpart or version of it.

Actually, the answer appears to be in the first line of the message:
Unable to load DLL 'VCECLB.dll': The specified module could not be found. (Exception from HRESULT:0x8007007E
It says it cannot find the DLL, not that the DLL is not compatible.
The DLL should be located with the other DLLs with your program, and you need to add the DLL to the solution.
If by some chance the DLL is compiled as Native C/C++ (i.e., Not .NET style), it can be called from a C/C++ that is a .NET style, but not a C# routine.
So, if the problem is that the module can be found but not loaded, you need to write a C++ (.NET) routine to call from your C# routine (.NET) to call the C++ (Not .NET).
All code that talks directly to hardware must do this at some level. Most do this for you using multiple DLLs.

Related

How to use a dll generated in c# and compiled using Native AOT in other .NET Projects?

Since I first heard about the introduction of Native AOT feature in .NET 7 and that it was usable on console executables and class libraries, I wanted to try out on some library projects I already had. Then, rised a problem I can't seem to workaround no matter the amount of research made. In fact after compiling successfully into a native dll, I don't know how to use it inside other .Net projects as it is no longer recognized as a .Net library type to be added as reference.
If anyone could enlighten me on how to access the methods compiled in that native dll from any other project in .Net, this could really mean a lot to me.
I have tried using the export attribute on public methods in the original library code to make them visible to external call. And then using import attribute on the caller project but nothing seem to work I can't see any public method from the generated dll.

Problems while Importing a C++ managed dll to C#.NET Project

It seems like a duplicate question from the title, but let me clarify that I have already searched and followed different methods.
Basically I have to import a managed C++ dll into a C# project. I consulted some tutorials and finally created a managed C++ dll. Now while importing the dll I got the following error:
An attempt was made to load a program with an incorrect format
I found some solutions like this but these solutions could not help me. Before facing this error I had to consult this solution and then this one too.
Here's a screenshot of how I am trying to import the dll:
Settings:
Code:
Someone please guide me about the right way to import the dll in my project.
This C++ library is for certain architecture (x64 or x86). C# on the other hand is not(Any CPU), that is why this kind of error - C# most likely assumes You have a x64 unmanaged library. You should restrict the architecture in C# project to either of these (x64 or x86) so in both projects they are the same.
Should you not be copying the file as a pre-build step? Make sure it is in the correct location before the build. I would also remove the exit 0, it's not needed.
That DllImport is usual for native C-style APIs, not the managed and C++/CLI assemblies. These .Net assemblies, be they mixed mode or not, can be added in the usual assembly dependency locations.
If this is a "Managed C++" and not a "C++/CLI" assembly, it may also be incompatible with the target .Net framework.

How To Create A VB Backward Compatible ActiveX COM DLL (w/ DllRegisterServer Entry Point) In C#

I did look at the following question, however, it doesn't resolve my issue.
- Unable to register DLL using Regsv32 - error "Dll was loaded but the entry-point DllRegisterServer was not found"
Unable to register DLL using Regsv32 - error "Dll was loaded but the entry-point DllRegisterServer was not found"
The problem I am facing is that a new DLL created w/ C# has to literally work within a legacy POS system running in Windows XP .NET 4.0 that was originally created with Visual Basic 6.0, which automatically created all of the proper entry-points (such as DLLRegisterServer) within the DLL assembly. However, these ActiveX COM entries, since they are no longer used, are not created with C# in Visual Studio 2012/2013. So, my question is, How can I create a Windows XP .NET 4.0 (VB backward compatible) ActiveX COM DLL with C# in Visual Studio 2012/2013, to replace the original VB DLL ? Can these entries be entered manually (& successfully compiled) in the C# DLL (i.e., using ???.??? STDAPI DllRegisterServer(void) { return true; }) ?
Your terminology is a little confusing: there is no such thing as "Windows XP .NET 4.0", but it sounds like you want to use .NET 4.0 on Windows XP. That would be OK. Note that your POS must be running Windows XP SP3. Note that .NET 4.5 and above are not supported in Windows XP. Your new DLLs must be compiled under .NET 4.0 or earlier. There are special complications if your POS software already uses .NET for anything else. I'll assume here that you are using .NET 4.0 and that the POS doesn't otherwise depend on .NET.
You do need to install on the POS machines an appropriate version of the .NET runtime (the version under which your DLL has been compiled is the best choice). But after that - yes, of course RegAsm will allow you to register a COM DLL (not RegSvr32 however; RegSvr32 doesn't know how to register a .NET DLL). As I mentioned in a my previous comment, the details in the registry will necessarily be different from the details for the VB6 DLL.
The installers for the different supported .NET runtimes are available from Microsoft. There are two options:
The ".NET 4.0 Client Profile installer" is currently available here:
http://www.microsoft.com/en-us/download/details.aspx?id=24872
The "Full .NET installer" is currently available here:
http://www.microsoft.com/en-us/download/details.aspx?id=17718
If you can help it, you want to compile your DLL using the ".NET 4.0 Client Profile" option, which is a subset of the whole runtime with only the most commonly used features. The Client Profile is significantly smaller. You will know immediately if your application is compatible with the Client Profile simply by trying to compile it with that option. If you are using something that is not included in the Client Profile, your DLL will fail to compile. In that case, try first to see if you can accomplish the same functionality without using the non-client-profile feature. If you can't, then use the full runtime.
Now the hard part: you don't specify if your DLL exposes a bunch of custom interfaces, whether it only consumes interfaces declared by the application, or a combination of both. You also don't specify how the POS software knows about your DLL and whether it uses Automation (IDispatch) like VBScript would or "custom" interfaces, and you don't say who declares the interfaces. Setting up the appropriate interfaces in C#/VB.NET can be a tricky problem because VB6 doesn't exactly make it easy to look at the details.
You can pull the most critical information from the existing DLL by registering it and looking at the resulting classes via the "OLE View" tool. The rest of the details will depend on how the main POS application uses your DLL. The full details of what to do can be complex and are well outside of the scope of any one single answer in Stack Overflow.
So basically you are trying to create dotnet based COM component. Set your project property as COM visible and Register your assembly using Regasm
regasm myDotnetCOM.dll
OR
regasm /codebase myDotnetCOM.dll
Now you can use CreateObject for this COM component.

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.

ExecutionEngineException thrown when loading native dll

I have a 32-bit .net application that uses a native 32-bit DLL
via DllImport(). The native DLL is our internal file analysis library, and I need to use it as porting it to C# would be a problem if people update it (other software uses it).
The problem is that when I try to execute any method in the native DLL I get a System.ExecutionEngineException thrown. In fact, I've reduced the managed application to a simple tester that just calls a native method, but it still fails.
I am on 64-bit Windows 7, but that should not matter as I'm compiling everything as 32-bit binaries.
What is also interesting, when I look at the native DLL in the Dependency Walker, it shows that it can't find msvcr90.dll - but when I open any other of our native DLLs in the Dependency Walker, it can find their referenced msvcr90.dll just fine. Can there by some wrongness in the compilation of native DLL that messes up its DLL references?
OK - the problem was a little bit misty. Apparently a colleague modified the native DLL's method to contain one parameter with default value (that C++ handles just fine), but, as we know, C# 3.5 does not. After supplying a value for that parameter, everything worked fine!

Categories

Resources