Solved

Getting DLL To Work in Visual Studio

Posted on 2012-04-09
5
685 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
Comment Utility
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
Comment Utility
That is using Reflection which is dog slow and not really multithread friendly
0
 
LVL 23

Expert Comment

by:wdosanjos
Comment Utility
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 19

Expert Comment

by:Daniel Van Der Werken
Comment Utility
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
Comment Utility
Dynamic - forgot all about it - thanks!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Python Regex Problem 24 79
Visual Studio installation 5 32
Visual Studio: built-in keystroke automation 2 21
Form design in vb.net 7 17
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…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

763 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

12 Experts available now in Live!

Get 1:1 Help Now