DLL ��J������Developing DLLs

適用されますExcel 2013 |。Office 2013 |Visual StudioApplies to: Excel 2013 | Office 2013 | Visual Studio

���C�u�����Ƃ́A���s�”\�A�v���P�[�V�����ɂ����‚��̋@�\��f�[�^��񋟂���R���p�C���ς݃R�[�h�̏W���ł��B���C�u�����͐ÓI�܂��͓��I�Ƀ����N�����邱�Ƃ��”\�ŁA�]���A�t�@�C�����̊g���q�͂��ꂼ�� .lib ����� .dll �ƂȂ��Ă��܂��B�ÓI���C�u���� (C �����^�C�� ���C�u�����Ȃ�) �̓R���p�C�����ɃA�v���P�[�V�����Ƀ����N����A���ʓI�Ɏ��s�”\�v���O�����̈ꕔ�ƂȂ�܂��B�A�v���P�[�V�����́A�K�v�ȂƂ� (�ʏ�́A�A�v���P�[�V�����̋N����) �� DLL ��ǂݍ��݂܂��BDLL �́A�ʂ� DLL ��ǂݍ��ݓ��I�Ƀ����N�ł��܂��BA library is a body of compiled code that provides some functionality and data to an executable application. Libraries can be either statically linked or dynamically linked, and they conventionally have the file name extensions .lib and .dll respectively. Static libraries (such as the C run-time library) are linked to the application at compilation and so become part of the resulting executable. The application loads a DLL when it is needed, usually when the application starts up. One DLL can load and dynamically link to another DLL.

Dll を使用する利点Benefits of using DLLs

DLL �̎�ȗ��_�͎��̂Ƃ���ł��BThe main benefits of DLLs are as follows:

  • ���ׂẴA�v���P�[�V�������A�f�B�X�N��̒P��R�s�[����L�ł��܂��BAll applications can share a single copy on disk.

  • �A�v���P�[�V�����̎��s�”\�t�@�C������K�͂Ȃ�̂ɂł��܂��BApplications' executable files are kept smaller.

  • ��K�͂ȊJ���v���W�F�N�g��ו����ł��܂��B�A�v���P�[�V������ DLL �̊J���҂����ӂ���K�v������̂́A���ꂼ��̕����Ԃ̃C���^�[�t�F�C�X�݂̂ɂȂ�܂��B���̃C���^�[�t�F�C�X�́ADLL �ɂ���ăG�N�X�|�[�g����܂��BThey enable large development projects to be broken down. Application and DLL developers only need agree an interface between their respective parts. This interface is exported by the DLL.

  • DLL �̊J���҂� DLL ��X�V�ł��܂��B�����̏ꍇ�A���̖ړI�͌���������コ������A�o�O��C�������肷�邱�Ƃɂ���܂��BDLL �̃G�N�X�|�[�g���ꂽ�C���^�[�t�F�C�X��ύX���Ȃ��ꍇ�ɂ́ADLL ��g�p���Ă��邷�ׂẴA�v���P�[�V������X�V����K�v�͂���܂���BDLL developers can update DLLs—perhaps to make them more efficient or to fix a bug—without having to update all the applications that use it, provided that the exported interface of the DLL does not change.

DLL ��g�p����ƁAMicrosoft Excel �Ƀ��[�N�V�[�g�֐��ƃR�}���h��lj��ł��܂��BYou can use DLLs to add worksheet functions and commands in Microsoft Excel.

Dll を作成するためのリソースResources for creating DLLs

DLL ��쐬����ɂ́A�ȉ����K�v�ƂȂ�܂��BTo create a DLL, you need the following:

  • �\�[�X �R�[�h �G�f�B�^�[�BA source code editor.

  • �\�[�X �R�[�h��A�g�p���Ă���n�[�h�E�F�A�ƌ݊����̂���I�u�W�F�N�g �R�[�h�ɕϊ�����R���p�C���BA compiler to turn source code into object code that is compatible with your hardware.

  • �g�p���Ă���ꍇ�ɐÓI���C�u��������R�[�h��lj�������A���s�”\ DLL �t�@�C����쐬�����肷�邽�߂̃����J�[�BA linker to add code from static libraries, where used, and to create the executable DLL file.

Microsoft Visual Studio �Ȃǂ̍ŐV�̓����J���‹� (IDE) �ɂ́A����炷�ׂĂ�������Ă��܂��B�܂��A����ɑ����̂�́A�‚܂�X�}�[�g �G�f�B�^�[�A�R�[�h��f�o�b�O���邽�߂̃c�[���A�����̃v���W�F�N�g��Ǘ����邽�߂̃c�[���A�V�����v���W�F�N�g �E�B�U�[�h�A����ё��̏d�v�ȑ����̃c�[�������܂��BModern integrated development environments (IDEs), such as Microsoft Visual Studio, provide all of these things. They also provide a great deal more: smart editors, tools to debug your code, tools to manage multiple projects, new project wizards, and many other important tools.

DLL �́AC/C++�APascal�AVisual Basic �Ȃǂ����‚��̌���ō쐬�ł��܂��BExcel �ɗp�ӂ���Ă��� API �̃\�[�X �R�[�h�� C ����� C++ �ł��邱�Ƃ���A���̎����ł͂���� 2 �‚̌���ɂ‚��Ă̂ݎ��グ�܂��BYou can create DLLs in several languages, for example, C/C++, Pascal and Visual Basic. Given that the API source code provided with Excel is C and C++, only these two languages are considered in this documentation.

関数とコマンドをエクスポートします。Exporting functions and commands

DLL �v���W�F�N�g��R���p�C������Ƃ��A�R���p�C���ƃ����J�[�́A�G�N�X�|�[�g�Ώۂ̊֐���c�����ăA�v���P�[�V�������g�p�ł���悤�ɂ���K�v������܂��B���̃Z�N�V�����ł́A���̂��߂̕��@�ɂ‚��Đ�����܂��BWhen compiling a DLL project, the compiler and linker need to know what functions are to be exported so that they can make them available to the application. This section describes the ways this can be done.

�R���p�C�����\�[�X �R�[�h��R���p�C������Ƃ��A�ʏ�A�R���p�C���͊֐��̖��O��A�\�[�X �R�[�h�Ŏ�����Ă��閼�O����ύX���܂��B�����̏ꍇ�A���̂��߂ɂ͖��O�̐擪�܂��͖����ɒlj�����܂��B���̃v���Z�X�́A���O�f�R���[�g�ƌĂ΂�܂��B�֐����ADLL ��ǂݍ��ރA�v���P�[�V�����ŔF���”\�Ȗ��O�ŃG�N�X�|�[�g����邱�Ƃ�m�F����K�v������܂��B�‚܂�A�f�R���[�g���ꂽ���O�ƊȒP�ȃG�N�X�|�[�g����֘A�t����悤�Ƀ����J�[�Ɏw�����܂��B�G�N�X�|�[�g���́A�\�[�X �R�[�h�Ɏ�����Ă��錳�̖��O�ɂ��邱�Ƃ�A���̖��O�ɂ��邱�Ƃ�ł��܂��BWhen compilers compile source code, in general, they change the names of the functions from their appearance in the source code. They usually do this by adding to the beginning and/or end of the name, in a process known as name decoration. You need to make sure that the function is exported with a name that is recognizable to the application loading the DLL. This can mean telling the linker to associate the decorated name with a simpler export name. The export name can be the name as it originally appeared in the source code, or something else.

���O��f�R���[�g������@�́A����A����уR���p�C���Ɏw������Ă���֐���g�p�ł���悤�ɂ�����@�A�‚܂��Ăяo���K���ɂ���ĈقȂ�܂��BDLL ���g�p���� Windows �W���̃v���Z�X�ԌĂяo���K��́AWinAPI �K��ƌĂ΂�Ă��܂��B����́AWindows �w�b�_�[ �t�@�C���� WINAPI �Ƃ��Ē�**** ����܂��BThe way the name is decorated depends on the language and how the compiler is instructed to make the function available, that is, the calling convention. The standard inter-process calling convention for Windows used by DLLs is known as the WinAPI convention. It is defined in Windows header files as WINAPI, which is in turn defined using the Win32 declarator __stdcall.

(かどうかは、ワークシート関数、マクロ シートと同じ関数、またはユーザー定義のコマンド) を Excel で使用する DLL のエクスポート関数は、はい常に使用する必要があります / _ _stdcall呼び出し規約です。A DLL-export function for use with Excel (whether it is a worksheet function, macro-sheet equivalent function, or user-defined command) should always use the WINAPI / __stdcall calling convention. Win32 コンパイラの既定の設定は、呼び出し規約が指定されていない場合にWINAPIV、としても定義されて、 _ _cdeclを使用すると関数の定義で明示的にはい指定子を含める必要があります。It is necessary to include the WINAPI specifier explicitly in the function's definition as the default in Win32 compilers is to use the __cdecl calling convention, also defined as WINAPIV, if none is specified.

�����J�[�ɑ΂��āA�֐����G�N�X�|�[�g����邱�ƁA���O�����̕��@�̂����ꂩ�ɂ���ĊO���ŔF������邱�Ƃ�ʒm�ł��܂��BYou can tell the linker that a function is to be exported, and the name it is to be known by externally in one of several ways:

  • �Ώۂ̊֐���ADEF �t�@�C���� EXPORTS �L�[���[�h�̌�ɔz�u���A�����N���ɂ��̃t�@�C����Q�Ƃ���悤 DLL �v���W�F�N�g�ݒ��s���܂��BPlace the function in a DEF file after the EXPORTS keyword, and set your DLL project setting to reference this file when linking.

  • �֐��̒�`�� __declspec(dllexport) �錾�q��g�p���܂��BUse the __declspec(dllexport) declarator in the function's definition.

  • #pragma �v���v���Z�b�T �f�B���N�e�B�u��g�p���āA�����J�[�Ƀ��b�Z�[�W�𑗐M���܂��BUse a #pragma preprocessor directive to send a message to the linker.

プロジェクトで使用できるすべての 3 つの方法であり、コンパイラとリンカーをサポートして、ください 1 つの方法は、次のいずれかの複数の関数をエクスポートするのには。Although your project can use all three methods and your compiler and linker support them, you should not try to export one function in more than one of these ways. たとえば、2 つのソース コード モジュール、1 つ C および C++ では、エクスポートする 2 つの関数を含む、DLL が含まれている 、_C_エクスポート、_Cpp_エクスポートそれぞれ。For example, suppose that a DLL contains two source code modules, one C and one C++, which contain two functions to be exported, my_C_export and my_Cpp_export respectively. わかりやすくするため、各関数は 1 つの倍精度数値の引数を受け取るし、同じデータ型を返すとします。For simplicity, suppose that each function takes a single double-precision numerical argument and returns the same data type. 次のセクションでは、これらの各メソッドを使用して各関数をエクスポートするための選択肢が記載されています。The alternatives for exporting each function by using each of these methods are outlined in the following sections.

DEF ファイルを使用します。Using a DEF file

double WINAPI my_C_export(double x)
{
/* Modify x and return it. */
    return x * 2.0;
}
double WINAPI my_Cpp_export(double x)
{
// Modify x and return it.
    return x * 2.0;
}

DEF �t�@�C���ɂ́A���̍s��܂߂�K�v������܂��BThe DEF file would then need to contain these lines.

EXPORTS my_C_export = _my_C_export@8 my_Cpp_export


EXPORTS �X�e�[�g�����g�̌�̍s�̈�ʓI�ȍ\���́A���̂悤�ɂȂ�܂��BThe general syntax of a line that follows an EXPORTS statement is as follows.

entryname[=internalname] [@ordinal[NONAME]] [DATA] [PRIVATE]

C �֐��̓f�R���[�g����܂����ADEF �t�@�C���̓����J�[�ɑ΂��āA(���̗�̏ꍇ) ���̃\�[�X �R�[�h����g�p���Ċ֐�����J���邱�Ƃ𖾎��I�ɋ�������_�ɒ��ڂ��Ă��������B�����J�[�͌��̃R�[�h����g�p���� C++ �֐���ÖٓI�ɃG�N�X�|�[�g����̂ŁADEF �t�@�C���Ƀf�R���[�g���ꂽ���O��܂߂�K�v�͂���܂���BNote that the C function has been decorated, but the DEF file explicitly forces the linker to expose the function using the original source code name (in this example). The linker implicitly exports the C++ function using the original code name, so that it is not necessary to include the decorated name in the DEF file.

32 �r�b�g�� Windows API �֐��Ăяo���̏ꍇ�AC �R���p�C���֐��̃f�R���[�g�̋K��͎��̂悤�ɂȂ�܂��B function_name �� _ function_name@ n �ɂȂ�܂��B�����ŁA n �́A���ׂĂ̈����Ŏ���� 10 �i�̃o�C�g���ł��B�e�o�C�g���́A�ł�߂� 4 �̔{���ɐ؂�グ���܂��BFor 32-bit Windows API function calls, the convention for the decoration of C-compiled functions is as follows: function_name becomes _ function_name@ n where n is the number of bytes expressed as a decimal taken up by all the arguments, with the bytes for each rounded up to the nearest multiple of four.

注意

Win32 �ł́A���ׂẴ|�C���^�[�� 4 �o�C�g�P�ʂł��B�߂�l�̌^�́A���O�f�R���[�g�ɉe����^���܂���BAll pointers are four bytes wide in Win32. The return type has no impact on name decoration.

C++ �R���p�C���ɑ΂��āAC++ �֐��̃f�R���[�g����Ă��Ȃ����O����J����悤�ɋ������邱�Ƃ��ł��܂��B���̏ꍇ�ɂ́A���̗�Ɏ�����Ă���悤�ɁAextern"C"{...} �u���b�N��Ɋ֐�����ъ֐��v���g�^�C�v�����܂��B(�����ł͒������� {} �͏ȗ�����Ă��܂��B�錾���Q�Ƃ���̂́A����̊֐��R�[�h �u���b�N�݂̂�����ł�)�BIt is possible to force the C++ compiler to expose undecorated names for C++ functions by enclosing the function, and any function prototypes, within an extern "C" {…} block, as shown in this example. (The braces {} are omitted here because the declaration only refers to the function code block that immediately follows it).

extern "C"
double WINAPI my_undecorated_Cpp_export(double x)
{
// Modify x and return it.
    return x * 2.0;
}

C �܂��� C++ �\�[�X �t�@�C���Ɋ܂߂邱�Ƃ��ł���w�b�_�[ �t�@�C���� C �֐��v���g�^�C�v��z�u����ꍇ�A���̃v���v���Z�b�T �f�B���N�e�B�u��܂߂�K�v������܂��BWhen you are placing C function prototypes in header files that could be included in C or C++ source files, you should include the following pre-processor directive.

#ifdef __cplusplus
extern "C" {
#endif
double WINAPI my_C_export(double x);
double WINAPI my_Cdecorated_Cpp_export(double x);
#ifdef __cplusplus
}
#endif

関数宣言子を使用します。Using the __declspec(dllexport) declarator

__declspec(dllexport) �L�[���[�h��A���̂悤�Ɋ֐��̐錾�Ŏg�p�ł��܂��BThe __declspec(dllexport) keyword can be used in the declaration of the function as follows.

__declspec(dllexport) double WINAPI my_C_export(double x)
{
/* Modify x and return it. */
    return x * 2.0;
}

宣言の極端な左の方式を追加する必要があります。The __declspec(dllexport) keyword must be added at the extreme left of the declaration. このアプローチの利点は、関数が定義ファイルに登録する必要がないことは、エクスポートの状態の定義を使用して右です。The advantages of this approach are that the function does not need to be listed in a DEF file, and that the export status is right with the definition.

C++ �֐��� C++ ���O�f�R���[�g�ɂ���Ďg�p�”\�ɂȂ�̂������ꍇ�ɂ́A���̂悤�Ɋ֐���錾���Ȃ���΂Ȃ�܂���BIf you want to avoid a C++ function being made available with the C++ name decoration, you must declare the function as follows.

extern "C"
__declspec(dllexport) double WINAPI my_undecorated_Cpp_export(double x)
{
// Modify x and return it.
    return x * 2.0;
}

�����J�[�͂��̊֐��� my_undecorated_Cpp_export �Ƃ��Ďg�p�ł���悤�ɂ��܂��B�‚܂�A�f�R���[�g�Ȃ��ŁA�\�[�X �R�[�h�Ŏ�����Ă���܂܂̖��O�ɂȂ�܂��BThe linker will make the function available as my_undecorated_Cpp_export, that is, the name as it appears in the source code with no decoration.

リンカーのプリプロセッサ ディレクティブ #pragma を使用してください。Using a #pragma preprocessor linker directive

最近のバージョンの Microsoft Visual Studio は、2 つの定義済みマクロをサポートするには、 #pragmaディレクティブと組み合わせて使用する場合を使用すると、関数のコード内から直接関数をエクスポートするようにリンカーに指示します。Recent versions of Microsoft Visual Studio support two predefined macros that, when used in conjunction with a #pragma directive, enable you to instruct the linker to export the function directly from within the function code. マクロは、関数__および__FUNCDNAME (二重下線の各端点に注意してください) は、装飾されていないと、装飾された関数名をそれぞれ展開。The macros are FUNCTION and FUNCDNAME (note the double underline at each end) which are expanded to the undecorated and decorated function names respectively.

���Ƃ��΁AMicrosoft Visual Studio ��g�p���Ă���ꍇ�A�����̍s����̂悤�ɋ��ʃw�b�_�[ �t�@�C���ɑg�ݍ��ނ��Ƃ��ł��܂��BFor example, when you are using Microsoft Visual Studio, these lines can be incorporated into a common header file as follows.

#if _MSC_VER > 1200 // Later than Visual Studio 6.0
#define EXPORT comment(linker, "/EXPORT:"__FUNCTION__"="__FUNCDNAME__)
#else // Cannot use this way of exporting functions.
#define EXPORT
#endif // else need to use DEF file or __declspec(dllexport)

���̃w�b�_�[���\�[�X �t�@�C���Ɋ܂܂�Ă���ƁA2 �‚̊֐������̂悤�ɃG�N�X�|�[�g�ł��܂��BIf this header is included in the source files, the two example functions can then be exported as follows.

C �R�[�h:C code:

double WINAPI my_C_export(double x)
{
#pragma EXPORT
/* Modify x and return it. */
    return x * 2.0;
}

C++ �R�[�h:C++ code:

double WINAPI my_Cpp_export(double x)
{
#pragma EXPORT
// Modify x and return it.
    return x * 2.0;
}

���̃f�B���N�e�B�u�́A�֐��̖{�̓�ɔz�u����K�v������A�R���p�C�� �I�v�V���� /EP �܂��� /P ���ݒ肳��Ă��Ȃ��ꍇ�ɂ̂݊g������܂��B���̎�@��g�p����ƁADEF �t�@�C���܂��� __declspec(dllexport) �錾���K�v�Ȃ��Ȃ�A�֐��R�[�h�ŃG�N�X�|�[�g��Ԃ̎d�l��ێ��ł��܂��BNote that the directive must be placed within the body of the function and is only expanded when neither of the compiler options /EP or /P is set. This technique removes the need for a DEF file, or __declspec(dllexport) declaration, and keeps the specification of its export status with the function code.

�֘A����See also