Silverlight-Use WebService to Call DLL File Written by C++, Delphi in Silverlight

We can’t call DLL directly in Silverlight (not Out of Browser mode). But we need to load DLL in  many calculations or other applications, for example, calling DLL to distinguish signal which is sent by ID card reader or contact with program data written by Delph.

In this post, I will talk about writing a DLL file and customize a GetNum function to get sum of two parameter through calling this DLL file.

Firstly, we need to write a DLL file writen by C++ and name it as IlasLinkDll.dll. The following shows the important part code.


#defineMYLIBDLL extern “C” _declspec(dllimport)


#defineMYLIBDLL extern “C” _declspec(dllexport)


MYLIBDLL double GetNum(double Anum,double Bnum);

doubleGetNum(double Anum,double Bnum)


     return Anum+Bnum;


Then, we create a silverlight application program and name it SLLinkDLL. Add IlasLinkDll.dll as reference. Create a new web service file of Wservice.asmx. Copy following code in this file and add using System.Runtime.InteropServices; as reference.


        public string GetNumber(double A,double B)


            return GetNum(A, B).ToString() ;



        ///Get Dll Value


        ///<param name=”Anumber”>NumberA</param>

        ///<param name=”Bnumber”>NumberB</param>


        [DllImport(“IlasLinkDll.dll”, CharSet = CharSet.Ansi, EntryPoint = “GetNum”, ExactSpelling = false)]

        public static extern double GetNum(double Anumber, double Bnumber);

Finally, right click project name. Add service reference and add http://localhost:4389/Wservice.asmx. Copy the following code in MainPage.xaml.cs and GetNumber method in WebService can be called. Also, sum of two numbers can be gotten and displayed.

        public MainPage()



            //Create Object Instance of WebService Client

            WServiceSoapClient sclient=new WServiceSoapClient();

            //Call GetNumber Method and Send Two Parameters


            sclient.GetNumberCompleted += new EventHandler<GetNumberCompletedEventArgs>(sclient_GetNumberCompleted);         


        void sclient_GetNumberCompleted(object sender, GetNumberCompletedEventArgs e)


            //Result: 523



We export two parameter, 500 and 23 through the above code. Then, get a popup window to display the result, 523. The following code shows usage of DllImport parameter to load DLL.

[DllImport(“IlasLinkDll.dll”, CharSet = CharSet.Ansi, EntryPoint = “GetNum”, ExactSpelling = false)]

  • CallingConvention: indicates calling convention of entry point. If not indicating CallingConvention, use default CallingConvention.Winapi.
  • CharSet: indicates character set of entry point. If not indicating CharSet, use default CharSet.Auto.
  • EntryPoint: gives name of entry point in dll. If not indicating EntryPoint, use the original method name.
  • ExactSpelling: indicates if Entry Point must match exactly with spelling of entry point. If not indicating ExactSpelling, use default value false.
  • PreserveSig: indicates if signature can be remain or convert. When converted, it get a return value HRESULT and retval (one additional export parameter of HRESULT) signature. If not indicating PreserveSig, use default value true.
  • SetLastError: indicates if method remains Win32 “last error”. If not indicating SetLastError, use default value false.

Tip: It is possible that we cannot load a third party DLL file no mater what EntryPoint and CharSet indicated. At that time, we can write a DLL file using C++ to load the third party DLL which cannot be distinguished in C#. Then, call own DLL file in C#.

Two Components Recommend:

Spire.Doc, is used to operate MS Word document for .Net and Silverlight, including basic manipulations (generate, open, edit documents), mail merge and other Word functions manipulations.

Spire.XLS, is used to operate MS Excel for .NET and Silverlight, including basic manipulations (generate, open, edit files), chart creation and data exporting.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s