4 Replies - 14608 Views - Last Post: 04 June 2009 - 06:53 AM Rate Topic: -----

#1 Smjert  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 23-September 08

Dynamically load unmanaged dll

Posted 03 June 2009 - 04:38 PM

What i want to do is a program (in C#) that can be extended with plugins (in native C++).
The problem is that i don't know how to dynamically load an unmanaged dll..
I read about mixing unmanaged and managed on the dll, so theorically i can load the "managed" dll with LINQ, but the problem is to prepare accordingly the dll, since i want that the implementation part is in native C++.
What i was doing is this (this is the header):

#include <string>

using namespace std;

#pragma unmanaged
class SUhiddenClass
{
public:
	string Test();
};

#pragma managed
interface class IWrapper 
{
	 property SUhiddenClass hc;
};

public class MyWrapper : public IWrapper
{
private:
	SUhiddenClass m_hc;
public:
	MyWrapper();
	property SUhiddenClass hc
	{
		SUhiddenClass get(){return m_hc};
		SUhiddenClass set(SUhiddenClass value){m_hc = value};
	}
};


So i thought that in some way i can use MyWrapper.. or the interface IWrapper from C# to call Test function.. but obviously it doesn't compile because property declaration can be done only in managed type.
I'm really lost...

Is This A Good Question/Topic? 0
  • +

Replies To: Dynamically load unmanaged dll

#2 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 929
  • View blog
  • Posts: 6,316
  • Joined: 18-October 08

Re: Dynamically load unmanaged dll

Posted 03 June 2009 - 06:03 PM

You can call a native dll using the [DllImport("yourdll.dll")] attribute. That would be a good starting point to look at.
Was This Post Helpful? 0
  • +
  • -

#3 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 173
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: Dynamically load unmanaged dll

Posted 03 June 2009 - 06:16 PM

When you say you want to load the dll dynamically, do you mean you want to load the file at runtime? If so, this should help.

This post has been edited by lesPaul456: 03 June 2009 - 06:22 PM

Was This Post Helpful? 0
  • +
  • -

#4 Smjert  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 23-September 08

Re: Dynamically load unmanaged dll

Posted 04 June 2009 - 03:37 AM

View PostlesPaul456, on 3 Jun, 2009 - 05:16 PM, said:

When you say you want to load the dll dynamically, do you mean you want to load the file at runtime? If so, this should help.


Thank you, yes what i want to do is something like late binding, so load the dll at runtime.
Yesterday after hours of search i find also that page.. the problem now is that i tried to do that, but it doesn't work.
Don't know, probably is because it's a class function, now my C++ code looks like that (i changed to int the type returned by the function since extern "C" doesn't support string.. but when i'll find a solution to export i also need to do that with string type functions and etc, not supported by C):

Header

#include <string>
#define DllExport _declspec(dllexport)

using namespace std;

extern "C" DllExport int Test();

class SUInterface
{
	public:
		virtual int Test() = 0;
};

class MyClass : public SUInterface
{
	public:
		MyClass();
		int Test();
};


Module

#include "SUInterface.h"

using namespace std;

int MyClass::Test()
{
	return 1;
}



And here the C# part:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace SoftwareUpdater
{
	public partial class Form1 : Form
	{
		[DllImport("kernel32.dll")]
		internal static extern IntPtr LoadLibrary(String dllname);

		[DllImport("kernel32.dll")]
		internal static extern IntPtr GetProcAddress(IntPtr hModule, String procname);

		[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
		internal delegate int TestDel();
		public Form1()
		{
			InitializeComponent();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			IntPtr ptrDll = LoadLibrary(@"D:\Active Project\SoftwareUpdater\SUDll\Release\SUDll.dll");
			IntPtr procaddr = GetProcAddress(ptrDll, "Test");
			TestDel tst = (TestDel)Marshal.GetDelegateForFunctionPointer(procaddr, typeof(TestDel));
			textBox1.Text = tst().ToString();
		} 
	}
}

Was This Post Helpful? 0
  • +
  • -

#5 Smjert  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 23-September 08

Re: Dynamically load unmanaged dll

Posted 04 June 2009 - 06:53 AM

I solved.
I found this page and i did the same thing.
My C++ wrapper inherit from that C# interface and from the other C# project i load the semi-managed C++ dll with LINQ method.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1