Amit Agrawal
Amit Agrawal

Reputation: 41

Importing DLL functions in c# code

I have a DLL, whose functions i want to use in my c# code Here are the functions of that DLL :

extern "C"
{
  __declspec(dllimport)
  const char* __stdcall ZAJsonRequestA(const char *szReq);

  __declspec(dllimport)
  const wchar_t* __stdcall ZAJsonRequestW(const wchar_t *szReq);

  __declspec(dllimport)
  const BSTR __stdcall ZAJsonRequestBSTR(BSTR sReq);
}

Can anyone tell me how to use it in c# project, as this dll seems to be in other language ?

Upvotes: 4

Views: 24174

Answers (2)

Patrick D'Souza
Patrick D'Souza

Reputation: 3573

Please have a look at the following article on Code Project for an in depth explanation

A small sample from the linked article is as shown below

To call a function, say methodName

int __declspec(dllexport) methodName(int b)
{
      return b;
}

Include the class library (MethodNameLibrary.dll) containing the above method as shown below in c#

class Program
{
   [DllImport(@"c:\MethodNameLibrary.dll")]
   private static extern int methodName(int b);
   static void Main(string[] args)
   {
       Console.WriteLine(methodName(3));
   }
}

Upvotes: 4

Spook
Spook

Reputation: 25927

Use DllImport and MarshalAs attributes.

Upvotes: 0

Related Questions