Solved

Getting DLL To Work in Visual Studio

Posted on 2012-04-09
5
692 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

821 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