Solved

Getting DLL To Work in Visual Studio

Posted on 2012-04-09
5
690 Views
Last Modified: 2012-08-13
Through Guru.com, I had a developer create a DLL for me to include in a Visual Studio Windows Service project.  To test out his DLL, this developer included an EXE that would register the DLL on the system and he included the following code embedded in an Excel spreadsheet button's click event to call his object:

Set myObject = CreateObject("MyComponent")
    
    resultStatus = myObject.doWork()
    MsgBox resultStatus 

Open in new window


I need to have this work in Visual Studio - ideally without using Reflection - and so far I have fallen flat on my face.  Given the type of code, DLLImport is not working, I cannot directly reference the DLL as .NET does not view it as a valid COM object and registering the object does not appear to add the object to the list of available COM components.

The DLL itself is written in Python, wrapped in a C layer for portability, and I need to figure out how to make this things accessible in .NET.

Any ideas?
0
Comment
Question by:ktola
  • 2
  • 2
5 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 37824431
Here is the C# (.NET 4.0) version:
dynamic myObject = Activator.CreateInstance(Type.GetTypeFromProgID("MyComponent"));

resultStatus = myObject.doWork(); // assuming resultStatus is defined somewhere else
System.Windows.Forms.MessageBox.Show(resultStatus.ToString());

Open in new window

0
 

Author Comment

by:ktola
ID: 37824447
That is using Reflection which is dog slow and not really multithread friendly
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 37824462
I think that's your best chance to make it work with your DLL.  You could cache the MyObject instance to avoid the overhead.
0
 
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 37824495
If it's a COM DLL, then you would be able to use regsvr32.exe to register the DLL and wrap it in an Interop for your .NET project. However, I guess it isn't, right? If it's a C/C++ DLL that isn't COM based, then you would normally use DLLImport. Otherwise, I don't think there is any other way to incorporate the code into your project.

Can you give us more information? What do you mean DLLImport isn't working?

I think Activator.CreateInstance(...) is more the way you want to go, though.

These links might prove useful, but it does use reflection:
http://www.csharp-examples.net/reflection-examples/
http://social.msdn.microsoft.com/Forums/zh/csharpgeneral/thread/fe3d7977-52f5-45d9-b142-122e1c588485
0
 

Author Closing Comment

by:ktola
ID: 37824653
Dynamic - forgot all about it - thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now