1

I´m unexperienced with handling DllImport stuff, so I´d be glad if anyone of you can solve my little problem. I want to import a dll which has a method within a class. The method should return a stringarray.

So here´s some code:

Form1.cs (Calling Position):

...
 public partial class Form1 : Form
    {
        [DllImport("lang.dll")]
        public static extern string[] getValues();
        //                   |
        //error occures here v
        string[] labels = getValues();
        Status prgmStatus;

        public Form1()
        {
...

language.cs (Class of my .dll file):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace lang
{
    public class language
    {
        public language()
        {
        }

        public string[] getValues()
        {
            string[] content = 
            {
                "User",
                "Password",
                "Login",
                "Create new account ->",
                "Repeat password",
                "E-Mail adress",
                "Register",
                "<- Back to Login"
            };
            return content;
        }
    }
}

So when I start my program it calls the method of the dll and this appears:

(Image is found here:

How may i avoid this error and get the array properly?

Thanks for answers and solutions, Paedow

Update:

It should be possible to load any other .dll file from this path, with same structure but other content. The dll contains the labels for the windows form in the english language. when someone wants to have this program in his own language he has to compile a dll with his labels and just replace the dll.

Another Update: The dll file is not build in the same solution. The dll is an own solution, only the final .dll file will be used in my program, so there are no references.

Mohsen Safari
  • 6,669
  • 5
  • 42
  • 58
LostPhysx
  • 3,573
  • 8
  • 43
  • 73
  • 1
    You are using DllImport for a managed assembly.. DllImport is _meant_ to be used for unmanaged libraries. Have you considered using the many reflection-based methods of calling a managed assemblies code? – Simon Whitehead Sep 05 '12 at 22:41
  • You could also expose an Interface that can then be consumed via your other assembly – Paul Farry Sep 05 '12 at 22:52

2 Answers2

1

As you are working with managed dlls, you shouldn't use the [DllImport], which should be used only with unmanaged dlls (see here the distinction).

from now on, every time I mention a "dll" I mean a "managed dll"

If you want to compile your application with the dll (instead of loading it "manually" during execution, which I guess you don't want...) you can add a reference to:

  • The compiled dll file (".NET Reference");
  • The .dll project ("Project Reference"), only if the .dll project and the .exe project are in the same solution.

See here the official documentation on this. For more on solutions/projects you can start here.

Community
  • 1
  • 1
fableal
  • 1,577
  • 10
  • 24
0

You don't have to DllImport managed assembly as @Simon Whitehead said. Just reference it in your project and then

var l = new lang.language();
string[] labels = l.getValues();
Piotr Sobiegraj
  • 1,775
  • 16
  • 26