要望もあったので、これを機にDLLの作り方を勉強してみようと思い、作成しました。
他の環境で動くかわからないので、動作確認をしてくださると嬉しいです。
【DLLのダウンロード】(ヘッダファイル&インポートライブラリ同梱) 上のファイルにはDLLを作成するためのプロジェクトファイルも入っています。
ビルドには、このライブラリが必要となっています。
【a5ua C++ libraries】 ヘッダファイル(APIの定義)の内容とサンプルプログラムを載せておきます。
各APIはWINAPI風にハンドルを用いてアクセスします。
【a5ua/typing_engine_dll.h】
#pragma once
#ifdef A5UA_TYPING_ENGINE_DLL_EXPORT
# define A5UA_TYPING_ENGINE_DLL_API __declspec(dllexport)
#else
# define A5UA_TYPING_ENGINE_DLL_API __declspec(dllimport)
#endif
#ifndef __cplusplus
#include
#endif
// タイピングエンジンのハンドル定義
typedef struct a5uaHTYPING_ {int unused;} *a5uaHTYPING;
// 入力状況を取得するときに指定するパートを表す列挙型の定義
enum A5UA_TYPING_PROGRESS
{
A5UA_TYPING_PROGRESS_ACCEPTED, // 受理された部分
A5UA_TYPING_PROGRESS_TARGET, // 現在入力しようとしている部分
A5UA_TYPING_PROGRESS_REMAINDER // まだ入力されていない部分
};
// タイピングエンジンAPIの定義
#ifdef __cplusplus
extern "C" {
#endif
// タイピングエンジンの作成
// [in] path 設定ファイルのパス
// [ret] タイピングハンドル(設定ファイルが読み込めないなど、エラーが発生した場合はNULLポインタを返す)
A5UA_TYPING_ENGINE_DLL_API
a5uaHTYPING a5uaTypingCreateTyping(const char *path);
// タイピングエンジンの破棄
// [in] hTyping タイピングハンドル
A5UA_TYPING_ENGINE_DLL_API
void a5uaTypingDestroyTyping(a5uaHTYPING hTyping);
// タイプする文字列の設定
// [in] hTyping タイピングハンドル
// [ret] 成功したかどうかの真偽値
A5UA_TYPING_ENGINE_DLL_API
int a5uaTypingSetWord(a5uaHTYPING hTyping, const wchar_t *word);
// 一文字入力する
// [in] hTyping タイピングハンドル
// [ret] 入力を受理したかどうかの真偽値
A5UA_TYPING_ENGINE_DLL_API
int a5uaTypingInput(a5uaHTYPING hTyping, wchar_t c);
// 設定文字列部分の入力状況を取得
// [in] hTyping タイピングハンドル
// [in] part A5UA_TYPING_PROGRESS 列挙型の値を指定する
// [ret] 入力に応じた部分文字列
A5UA_TYPING_ENGINE_DLL_API
const wchar_t *a5uaTypingGetWordProgress(a5uaHTYPING hTyping, int part);
// ローマ字部分の入力状況を取得
// [in] hTyping タイピングハンドル
// [in] part A5UA_TYPING_PROGRESS 列挙型の値を指定する
// [ret] 入力に応じた部分文字列
A5UA_TYPING_ENGINE_DLL_API
const wchar_t *a5uaTypingGetInputProgress(a5uaHTYPING hTyping, int part);
// 設定した文字列をタイプし終わったかどうかの判定
// [in] hTyping タイピングハンドル
// [ret] 終わったかどうかの真偽値
A5UA_TYPING_ENGINE_DLL_API
int a5uaTypingFinished(a5uaHTYPING hTyping);
#ifdef __cplusplus
}
#endif
// Visual Studio なら、これでリンク可能
// 対応するDLLファイルは、EXEファイルから見えるところにおいてください
#ifdef _MT
# ifdef _DLL
# pragma comment(lib, "a5ua_typing_engine_dll-md.lib")
# else
# pragma comment(lib, "a5ua_typing_engine_dll-mt.lib")
# endif
#endif
#include
#include
#include
void PrintWordProgress(a5uaHTYPING hTyping)
{
const wchar_t *a = a5uaTypingGetWordProgress(hTyping, A5UA_TYPING_PROGRESS_ACCEPTED);
const wchar_t *t = a5uaTypingGetWordProgress(hTyping, A5UA_TYPING_PROGRESS_TARGET);
const wchar_t *r = a5uaTypingGetWordProgress(hTyping, A5UA_TYPING_PROGRESS_REMAINDER);
printf("[%S](%S)\n", a, r);
printf("[%S]{%S}\n", a, t);
}
void PrintInputProgress(a5uaHTYPING hTyping)
{
const wchar_t *a = a5uaTypingGetInputProgress(hTyping, A5UA_TYPING_PROGRESS_ACCEPTED);
const wchar_t *t = a5uaTypingGetInputProgress(hTyping, A5UA_TYPING_PROGRESS_TARGET);
const wchar_t *r = a5uaTypingGetInputProgress(hTyping, A5UA_TYPING_PROGRESS_REMAINDER);
printf("[%S](%S)\n", a, r);
printf("[%S]{%S}\n", a, t);
}
void PrintProgress(a5uaHTYPING hTyping)
{
PrintWordProgress(hTyping);
PrintInputProgress(hTyping);
puts("");
}
void Simulate(a5uaHTYPING hTyping, const wchar_t *word, const wchar_t *input)
{
if (!a5uaTypingSetWord(hTyping, word)) {
puts("error: ローマ字に変換できません");
}
PrintProgress(hTyping);
for ( ; !a5uaTypingFinished(hTyping) && *input; ++input) {
if (a5uaTypingInput(hTyping, *input)) {
PrintProgress(hTyping);
} else {
printf("miss!\n");
}
}
if (a5uaTypingFinished(hTyping)) {
printf("finished!\n\n");
} else {
printf("not finished!\n\n");
}
}
// 設定ファイルのパス
// このファイルには、カナ・記号とローマ字の対応関係が書かれている
#define TYPING_CONFIG_PATH "config.txt"
int main(void)
{
a5uaHTYPING hTyping;
// タイピングエンジンの作成
hTyping = a5uaTypingCreateTyping(TYPING_CONFIG_PATH);
if (!hTyping) {
fprintf(stderr, "タイピングエンジンの作成に失敗しました\n");
return -1;
}
// ロケールの設定
setlocale(LC_ALL, "japanese");
// いくつかシミュレーション
Simulate(hTyping, L"あかさたなはまやらわ", L"akasatanahamayarawa");
Simulate(hTyping, L"テストてすと", L"tesutotest");
Simulate(hTyping, L"とうきょうとっきょきょかきょく", L"toukixyoutoltukilyokyocakyocu");
// タイピングエンジンの破棄
a5uaTypingDestroyTyping(hTyping);
return 0;
}