HOWTO Implement LoadLibrary\GetProcAdrress\FreeLibrary in C#

G

Guest

HOWTO Implement LoadLibrary\GetProcAdrress\FreeLibrary in C#

Please help,

I want to fully implement LoadLibrary\GetProcAdrress\FreeLibrary in C#, and
be able to call functions that I use GetProcAddress on to pass info to
non-.NET apps. I can not use the standard "interop" process, as the DLL's are
created dynamically. I need this:

========================================

[DllImport("kernel32.dll")]
static extern ???HMODULE?? LoadLibrary
(
???LPCTSTR lpFileName???
);
[DllImport("kernel32.dll")]
static extern ???FARPROC?? GetProcAddress
(
???HMODULE hModule,???
???LPCSTR lpProcName???
);
[DllImport("kernel32.dll")]
static extern ???BOOL?? FreeLibrary
(
???HMODULE hModule???
);

// For clarification I CAN NOT USE the following:
//
// [DllImport("My.dll")]
// static extern ???BOOL?? MyFunc
// (
// ???LPCSTR pszParam1???, ???LPCSTR pszParam1???
// );
//
// I can not use this,

main()
{
// Code to create "My.dll"...
// ...
// ...
// ...

// My code actually creates the DLL called "My.dll" at run time. It does
not exist until
// AFTER the fictitious code above runs.

// Now load My.dll, get the MyFunc and run it.

???VAR??? ???VAR???_MyDll = LoadLibrary("My.dll");

???FUNCTION_PROTOTYPE??? ???FUNCTION_PROTOTYPE???_MyFunc;
???FUNCTION_PROTOTYPE???_MyFunc = (???) GetProcAddress(???VAR???_MyDll,
"MyFunc");

// Call My Func...

bool bRet = ???FUNCTION_PROTOTYPE???_MyFunc("Param_Number_1",
"Param_Number_2");

FreeLibrary(???FUNCTION_PROTOTYPE???_MyFunc);
}
 
N

Nicholas Paldino [.NET/C# MVP]

ATS,

If you are using .NET 2.0 or above, this is simple. You can declare
LoadLibrary and GetProcAddress to load your dll and get the address of your
function. Once you do that, you can pass the pointer to the function to the
static GetDelegateForFunctionPointer method and have it return your delegate
(you will have to create a delegate that has the appropriate signature).

You can get the declarations for GetProcAddress and LoadLibrary at
http://www.pinvoke.net

If you are running .NET 1.1 or before, then you will have to have an
unmanaged shim which will load the library for you, and return the function
pointer (which you can marshal as a delegate).

Hope this helps.
 
G

Guest

Thanks for the reply,

I plan to use .NET 2.0, but would to know 1.1 as well. As for that link, it
goes to a site that does not show code directly for this issue. Could you
please paste a sample in for at least 2.0 and 1.1 please?
 
N

Nicholas Paldino [.NET/C# MVP]

ATS,

You need to look up the functions you want to use in .NET there. You
would enter LoadLibrary in the search field, and it will show you the
declaration.
 
G

Guest

Thanks Nicholas for the reply, but that does not help. I've long since done
that which is why I'm here. Without this being spoon fed and spelled out
completely, I'll never get it to compile in C#. Right now for example, I get
the following error (compile time) for the following code:

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Reflection;

static extern IntPtr LoadLibrary(string csPath); // error CS0116: A
namespace does not directly contain members such as fields or methods

I've also tried this:

[DllImport("kernel32.dll")]
static extern IntPtr LoadLibrary(string csPath); // error CS1518: Expected
class, delegate, enum, interface, or struct

And I've tried MANY, MANY, MANY, MANY, MANY, MANY other things.
 
N

Nicholas Paldino [.NET/C# MVP]

ATS,

You need to place function definitions within the scope of a class. You
can't have functions outside of a class. This function would have no
implementation, just a declaration.
 
G

Guest

Well, I've found the disconnect to this issue, and a couple other derivations
of this same issue. The disconnect is that while my machine has .NET 2.0 on
it, and the .NET 2.0 SDK, My Visual Studio 2003 does not let me compile it.
More specifically, the Marshal.GetDelegateForFunctionPointer can not be
compiled. I've started another issue to ask specifically how (if possible) to
get VS 2003 to compile .NET 2.0.
 
W

Willy Denoyette [MVP]

ATS said:
Well, I've found the disconnect to this issue, and a couple other
derivations
of this same issue. The disconnect is that while my machine has .NET 2.0
on
it, and the .NET 2.0 SDK, My Visual Studio 2003 does not let me compile
it.
More specifically, the Marshal.GetDelegateForFunctionPointer can not be
compiled. I've started another issue to ask specifically how (if possible)
to
get VS 2003 to compile .NET 2.0.

You can't target v2.0 from VS2003, you need VS2005.

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top