Share c# 64位调用32位dll

phambaomama

New member
## C# 64 位 32 位 DLL

** DLL là gì? **

Thư viện liên kết động (DLL) là một thư viện các chức năng và dữ liệu có thể được sử dụng bởi nhiều chương trình cùng một lúc.DLL được lưu trữ trong một thư mục đặc biệt trên máy tính của bạn và được tải vào bộ nhớ khi một chương trình cần sử dụng chúng.Điều này cho phép các chương trình chia sẻ mã và dữ liệu, có thể tiết kiệm không gian và cải thiện hiệu suất.

** Cách gọi DLL 32 bit từ quy trình 64 bit trong C#? **

Có hai cách để gọi DLL 32 bit từ quy trình 64 bit trong C#:

1. ** Sử dụng thư viện Gọi nền tảng (PINVOKE). ** Thư viện pinvoke cho phép bạn gọi các chức năng từ một DLL được biên dịch cho một nền tảng khác.Để gọi DLL 32 bit từ quy trình 64 bit bằng Pinvoke, bạn cần phải thực hiện như sau:

1. Thêm tham chiếu sau đây vào dự án của bạn:

`` `
<Tham chiếu bao gồm = "System.Runtime.InteropService.dll" />
`` `

2. Xác định quy ước gọi cho chức năng bạn muốn gọi.Quy ước gọi cho hàm 32 bit là __stdcall.

3. Sử dụng phương thức Marshal.getProcaddress để lấy địa chỉ của hàm trong DLL.

4. Gọi chức năng bằng phương thức Marshal.invoke.

Dưới đây là một ví dụ về cách gọi DLL 32 bit từ quy trình 64 bit bằng Pinvoke:

`` `C#
[DllImport ("my32bit.dll", CallingConvent = CallingConvent.stdcall)]]
công khai extern int my32bitfunction (int a, int b);

int main ()
{
int result = my32Bitfunction (10, 20);
Console.WriteLine ("Kết quả là: {0}", kết quả);
}
`` `

2. ** Sử dụng lớp System.Reflection.Assembly. ** Lớp System.Reflection.Assugging cho phép bạn tải DLL vào bộ nhớ và truy cập các chức năng của nó.Để gọi một DLL 32 bit từ một quy trình 64 bit bằng lớp System.reflection.Assugging, bạn cần phải thực hiện như sau:

1. Tải DLL vào bộ nhớ bằng phương pháp ASSEM.LOADFILE.

2. Nhận loại chức năng bạn muốn gọi.

3. Nhận địa chỉ của hàm bằng phương thức MethodInfo.GetEntryPoint.

4. Gọi chức năng bằng phương thức MethodInfo.invoke.

Dưới đây là một ví dụ về cách gọi DLL 32 bit từ quy trình 64 bit bằng cách sử dụng lớp system.reflection.Assembly:

`` `C#
Lắp ráp lắp ráp = ASSEMBLY.LOADFILE ("my32bit.dll");
Loại loại = asion.gettype ("my32bitclass");
Phương thứcInfo Phương thức = type.getMethod ("My32BitFunction");
int result = (int) phương thức.invoke (null, đối tượng mới [] {10, 20});
Console.WriteLine ("Kết quả là: {0}", kết quả);
`` `

** Tôi nên sử dụng phương pháp nào? **

Thư viện Pinvoke là cách đơn giản hơn để gọi DLL 32 bit từ quy trình 64 bit.Tuy nhiên, lớp system.reflection.Assembly cho phép bạn kiểm soát nhiều hơn đối với việc tải và dỡ hàng DLL.Nếu bạn cần kiểm soát việc tải và dỡ tải DLL, thì bạn nên sử dụng lớp system.reflection.Assembly.

## 5 hashtag ở dạng #

* #csharp
* #64bit
* #32bit
* #Dll
* #pinvoke
=======================================
## C# 64 位调用 32 位 DLL

**What is DLL?**

A Dynamic Link Library (DLL) is a library of functions and data that can be used by multiple programs at the same time. DLLs are stored in a special folder on your computer and are loaded into memory when a program needs to use them. This allows programs to share code and data, which can save space and improve performance.

**How to call a 32-bit DLL from a 64-bit process in C#?**

There are two ways to call a 32-bit DLL from a 64-bit process in C#:

1. **Using the Platform Invoke (PInvoke) library.** The PInvoke library allows you to call functions from a DLL that is compiled for a different platform. To call a 32-bit DLL from a 64-bit process using PInvoke, you need to do the following:

1. Add the following reference to your project:

```
<Reference Include="System.Runtime.InteropServices.dll" />
```

2. Define the calling convention for the function you want to call. The calling convention for a 32-bit function is __stdcall.

3. Use the Marshal.GetProcAddress method to get the address of the function in the DLL.

4. Call the function using the Marshal.Invoke method.

Here is an example of how to call a 32-bit DLL from a 64-bit process using PInvoke:

```c#
[DllImport("my32bit.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int My32BitFunction(int a, int b);

int main()
{
int result = My32BitFunction(10, 20);
Console.WriteLine("The result is: {0}", result);
}
```

2. **Using the System.Reflection.Assembly class.** The System.Reflection.Assembly class allows you to load a DLL into memory and access its functions. To call a 32-bit DLL from a 64-bit process using the System.Reflection.Assembly class, you need to do the following:

1. Load the DLL into memory using the Assembly.LoadFile method.

2. Get the type of the function you want to call.

3. Get the address of the function using the MethodInfo.GetEntryPoint method.

4. Call the function using the MethodInfo.Invoke method.

Here is an example of how to call a 32-bit DLL from a 64-bit process using the System.Reflection.Assembly class:

```c#
Assembly assembly = Assembly.LoadFile("my32bit.dll");
Type type = assembly.GetType("My32BitClass");
MethodInfo method = type.GetMethod("My32BitFunction");
int result = (int)method.Invoke(null, new object[] { 10, 20 });
Console.WriteLine("The result is: {0}", result);
```

**Which method should I use?**

The PInvoke library is the more straightforward way to call a 32-bit DLL from a 64-bit process. However, the System.Reflection.Assembly class gives you more control over the loading and unloading of the DLL. If you need to control the loading and unloading of the DLL, then you should use the System.Reflection.Assembly class.

## 5 Hashtags in the form of #

* #csharp
* #64bit
* #32bit
* #Dll
* #pinvoke
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top