Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Getting DLL To Work in Visual Studio

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

829 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