DLL import in C#

Asked

Viewed 1,171 times

0

I joined a recent project that uses a Nearby printer to print tax coupons.

The last one who worked on the project used a DLL (if I’m not mistaken in C++) that managed the printer.

In the project, this DLL is imported via the following command:

[DllImport(@"C:\InfoSAT\ptptrpro.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern Int32 SetProtocol(Int32 protocol);

When trying to execute the imported method, I am thrown a System.DllNotFoundException stating that the specified module could not be found.

I found curious this error because in the folder informed in Import has this DLL.

My guess is:

1- This project was done at the time using Visual C# 2010 Express, and I’m trying to open it in Visual Studio 2015, it may be happening some incompatibility;

2- This DLL may be corrupted (difficult but not impossible);

3 - Something else I couldn’t imagine.

Thank you to anyone who can help me.

  • 1

    See in the project if it is referenced, for that go to References in the VS project explorer... If she is not, add a reference to her... It will be even better than "importing at hand", because then Voce will have the methods via Intellisense. You also need to add the library via the 'using' command to access it.

  • Thanks for the answer, I tried to do this, but when trying to add a dll, it displays: The referent to 'C: Infosat ptrpro.ddl' could not be Added. Please make sure that the files is accessible, and that it is a Valid Assembly or COM Component.

  • 1

    Have to check the compatibility of the platform, try to compile in visual studio for x86 or x64

2 answers

2


It may be that one of the dll dependencies you are trying to reference is not being found.

A good program for checking dlls dependencies is the Dependencywalker that will list the dlls dependencies and which ones may be missing.

0

In Visual Studio, the solution manager has the item inserir a descrição da imagem aqui Right-click on it and click Add Reference. A references window will appear click the browse button and select your dll.

Depois de selecionar ela aparecerá neste menu, depois a selecone e as namespaces do c# serão carregadas

After selecting it will appear in this menu, then select it and the c# namespaces will be loaded after OK.

If you are making a c++ dll and using it in c project++, read this question I answered.

But if you use dll in both c++ to c# and vice versa, try to find some tutorial. I hope this helps.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.