logo search
CSharp_Prog_Guide

Задание точки входа

Точка входа определяет расположение функции в DLL. В управляемом проекте исходное имя или порядковый номер точки входа целевой функции определяет эту функцию в границах взаимодействия. Также разработчик может сопоставить точку входа с другим именем, фактически переименовывая функцию.

Ниже приведен перечень возможных причин переименования функции DLL:

В этом разделе показан способ переименования функции DLL в управляемом коде.

Переименование функции в C# и C++

Для задания функции по имени или порядковому номеру можно использовать поле DllImportAttribute..::.EntryPoint. Если имя функции в определении метода совпадает с именем точки входа в DLL, явно задавать функцию с помощью поля EntryPoint не требуется. В противном случае, чтобы указать имя или порядковый номер, следует использовать одну из следующих форм атрибута:

[DllImport("dllname", EntryPoint="Functionname")]

[DllImport("dllname", EntryPoint="#123")]

Обратите внимание, что порядковому номеру должен предшествовать знак #.

В следующем примере показан способ замены в программном коде MessageBoxA на MsgBox с помощью поля EntryPoint

-------

Calling a DLL Function

Although calling unmanaged DLL functions is nearly identical to calling other managed code, there are differences that can make DLL functions seem confusing at first. This section introduces topics that describe some of the unusual calling-related issues.

Passing Structures

Many unmanaged functions expect you to pass, as a parameter to the function, members of structures (user-defined types in Visual Basic) or members of classes that are defined in managed code. When passing structures or classes to unmanaged code using platform invoke, you must provide additional information to preserve the original layout and alignment. This topic introduces the StructLayoutAttribute attribute, which you use to define formatted types. For managed structures and classes, you can select from several predictable layout behaviors supplied by the LayoutKind enumeration.

Central to the concepts presented in this topic is an important difference between structure and class types. Structures are value types and classes are reference types — classes always provide at least one level of memory indirection (a pointer to a value). This difference is important because unmanaged functions often demand indirection, as shown by the signatures in the first column of the following table. The managed structure and class declarations in the remaining columns show the degree to which you can adjust the level of indirection in your declaration.

Unmanaged signature

Managed declaration:

no indirection

struct MyStruct(…);

Managed declaration:

one level of indirection

class MyStruct(…);

DoWork(MyStruct x);

Demands zero levels of indirection.

DoWork(ByVal x As MyStruct)

Adds zero levels of indirection.

Not possible because there is already one level of indirection.

DoWork(MyStruct* x);

Demands one level of indirection.

DoWork(ByRef x As MyStruct)

Adds one level of indirection.

DoWork(ByVal x As MyStruct)

Adds zero levels of indirection.

DoWork(MyStruct** x);

Demands two levels of indirection.

Not possible because ByRef ByRef cannot be used.

DoWork(ByRef x As MyStruct)

Adds one level of indirection.