2015.5.9 C # writing DLLs and C # calls C#dll

Source: Internet
Author: User

The procedure is simpler than the C # call Vc++dll.

First, create a DLL

New project, type select Class library, generate the result is DLL

Note: In the project Properties-Application Note three places, the assembly name and the default namespace can be adjusted, but to be consistent, other programs that call this DLL can be called by using a namespace, followed by the class name + function name. The output type keeps the default class library unchanged.

The DLL created by VC can be applied in this DLL, but at this point the DLL property can only be X86. Call the Vc++dll method or use the

[DllImport ("Space.dll")]

public static extern void Calccording (Double Jin, Double Wei, double-ang, double R, out double-ojin, out double owei);

DLL can be executed, delete files, and so on.

In addition to the DLL in order to get the local path of the DLL can not use the Application.executablepath () function of the amount, and to use the custom

private static string Getassemblypath ()

{

String _codebase = System.Reflection.Assembly.GetExecutingAssembly (). CodeBase;

_codebase = _codebase.substring (8, _codebase.length-8); 8 is the length of the file://

string[] arrsection = _codebase.split (new char[] {'/'});

String _folderpath = "";

for (int i = 0; i < arrsection.length-1; i++)

_folderpath + = Arrsection[i] + "/";

_folderpath = _folderpath.replace ("/", "\ \");

return _folderpath;

}

Second, C # call C#dll

1. The WindowsForm project can be added under the same solution as the DLL project and set as the startup project

2. Introduce the DLL created above into this project in a way that adds references

3, directly with the DLL's class name + function name method calls the DLL function (using a use namespace), without the [DllImport ("...")] method reference.

4. Parameter passing is more free, the parameter can be passed by array or list<> type.

2015.5.9 C # writing DLLs and C # calls C#dll

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.