Silverlight utility tips series: 22. Silverlight uses WebService to call C ++, DLL files compiled by Delphi [Download instance source code]

Source: Internet
Author: User

In SilverlightProgramDLL cannot be directly called in (non-out of browser mode), but we need to use DLL loading in many computing or other application calls. For example, Call DLL to identify the signal transmitted by the ID card reader, for example, to communicate with the program data compiled by a delph. In this article, we will write a simple DLL file and call this DLL to customize a getnum function to calculate the sum of the two parameters passed in.

First, we use vs2010 to compile a C ++ language DLL file named ilaslinkdll. dll (the source code for compiling this dll will also be included at the end of this chapter). The internal keyCodeAs follows:

 # Ifdef mylibdll
# Define Mylibdll extern "C" _ declspec (dllimport)
# Else
# Define Mylibdll extern "C" _ declspec (dllexport)
# Endif
Mylibdll Double Getnum ( Double Anum, Double Bnum );

Double Getnum ( Double Anum, Double Bnum)
{

Return Anum + Bnum;
}

Then we create a Silverlight application project named sllinkdll. In the sllinkdll. Web project, we reference the ilaslinkdll. dll file to create a web service file for wservice. asmx. Write the following code in this file and add reference of using system. runtime. interopservices:

 [Webmethod]
Public String Getnumber ( Double A, Double B)
{
Return Getnum (a, B). tostring ();

}
/// <Summary>
/// Obtain the DLL Value
/// </Summary>
/// <Param name = "anumber"> Number </Param>
/// <Param name = "bnumber"> Number B </Param>
/// <Returns> </returns>
[Dllimport ( " Ilaslinkdll. dll " , Charset = Charset. ANSI, entrypoint = " Getnum " , Exactspelling = False )]
Public Static Extern Double Getnum ( Double ANumber, Double Bnumber );

Finally, right-click the project name in the Silverlight Program-add service reference-add http: // localhost: 4389/wservice. asmx address. Write the following key code in the mainpage. XAML. CS file to call the getnumber method in WebService. Use DLL to calculate the sum between two numbers and return the result.

   Public  Mainpage ()
{
Initializecomponent ();
// Create an object instance of the WebService proxy class
Wservicesoapclient sclient = New Wservicesoapclient ();
// Call the getnumber method and pass two parameters.
Sclient. getnumberasync ( 500 , 23 );
Sclient. getnumbercompleted + = New Eventhandler < Getnumbercompletedeventargs > (Sclient_getnumbercompleted );

}

Void Sclient_getnumbercompleted ( Object Sender, getnumbercompletedeventargs E)
{
// The result is 523.
MessageBox. Show (E. Result );
}

The above code passes in two parameters: 500 and 23. Then, the pop-up window with the result 523 is displayed. Next, let's take a look at how to use the DLL dllimport feature parameters:

 [Dllimport (  "  Ilaslinkdll. dll  "  , Charset  =  Charset. ANSI, entrypoint  =     "  Getnum  "  , Exactspelling  =    False  )]

A. The callingconvention parameter indicates the call convention of the entry point. If no callingconvention is specified, use the default value callingconvention. winapi.
B. The charset parameter indicates the character set used in the entry point. If charset is not specified, the default value charset. Auto is used.
C. The entrypoint parameter specifies the name of the DLL entry point. If entrypoint is not specified, the method name is used.
D. The exactspelling parameter indicates whether the entrypoint must exactly match the spelling of the indicated entry point. If exactspelling is not specified, the default value is used. False .
E. The preservesig parameter indicates whether the method signature should be retained or converted. When the signature is converted, it is converted to a signature with an additional output parameter named retval that has the hresult return value and the return value. If preservesig is not specified, the default value is used. True .
The F and setlasterror parameters indicate whether the method retains Win32 " Previous Error " . If setlasterror is not specified, the default value is used. False .

Tip: No matter what entrypoint entry point and charset are specified in a project, when a third-party DLL file cannot be loaded, directly write a DLL file using C ++ to load the third-party DLL file that cannot be recognized and loaded in C #, and then call the self-compiled DLL file in C # to solve the problem.

The source code of this instance is compiled by vs2010 + silverlight4.0. Click sllinkdll.rar to download the source code of this instance. Click ilaslinkdll.rar to download the DLL file source code.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.