Interop DllImport in Class Library

G

Guest

Hello -

I am getting a compile error "Expected class, delegate, enum".

I am trying to create a Class Library in C# which wraps around an unmanaged
DLL that manages a MIDI interface.

In my Class Library code, I have tried to put a DllImport to the Win32
function MessageBox. Just to test out the DllImport functionality before I
add my custom functions that I want to wrap.

So in my Class Library code, I have

using System.Runtime.InteropServices;

namespace TestMod
{
[DllImport("user32.dll")]
public static extern int MessageBox( System.IntPtr h, string m, string
c, uint t);

........
}


I get an error on the work "int" with error "Expected class, delegate,
enum, interface or struct".

This works if my project target is a Windows exe.
In my case I would like to create a class library, so that many apps could
use this and not have to have a define section in the 'main' code.

Is the DllImport attribute allowed for a class library?

Much thx,
bruce
 
M

Marcel Beutner

just insert the dllimport into your class:


namespace ClassLibrary1
{

public class Class1
{
[DllImport("user32.dll")]
public static extern int MessageBox(System.IntPtr h, string m, string c,
uint t);

...
}
}

After that, you have to add a refernece to System.Windows.Forms


Marcel
 

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