すいません。動かないソースを書き込んでしまいました。
こっちなら動くと思います。
-----------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <io.h>
int main(int argc, char *argv[])
{
char buf[512];
char cmd[512];
char libfname[512];
char dllfname[512];
char *funcs[2048];
char *funcs2[2048];
char *p;
int nfuncs;
int i;
int j;
int len;
FILE *fp;
if (argc < 2) {
fprintf(stderr, "usage: mkimplib filename. ");
return EXIT_FAILURE;
}
if (_access(argv[1], 0) == -1) {
fprintf(stderr, "mkimplib: can not open %s. ", argv[1]);
perror("");
return EXIT_FAILURE;
}
sprintf(cmd, "dumpbin /exports %s", argv[1]);
fp = _popen(cmd, "r");
if (!fp) {
perror("mkimplib: popen error. ");
return EXIT_FAILURE;
}
for (i = 0, nfuncs = 0; ; i++) {
if (!fgets(buf, sizeof(buf), fp)) {
perror("mkimplib: dumpbin error. ");
return EXIT_FAILURE;
}
if (i > 18) {
if (buf[0] == '\n') break;
len = strlen(&buf[26]);
funcs[nfuncs] = (char *)malloc(len);
funcs2[nfuncs] = (char *)malloc(len);
memcpy(funcs[nfuncs], &buf[26], len);
funcs[nfuncs][len - 1] = '\0';
strcpy(funcs2[nfuncs], funcs[nfuncs]);
p = strrchr(funcs2[nfuncs], '@');
if (p) *p = '\0';
if (!(strcmp(funcs2[nfuncs], "alloca") == 0) &&
!(strcmp(funcs2[nfuncs], "setjmp") == 0) &&
!(strcmp(funcs2[nfuncs], "_alloca") == 0) &&
!(strcmp(funcs2[nfuncs], "_setjmp") == 0) &&
!(strcmp(funcs2[nfuncs], "atexit") == 0)) {
for (j = 0; j < nfuncs; j++) {
if (strcmp(funcs2[j], funcs2[nfuncs]) == 0) break;
}
if (j == nfuncs) nfuncs++;
}
}
}
_pclose(fp);
fp = fopen("__tmp__.cpp", "w");
if (!fp) {
perror("mkimplib: __tmp__.cpp open error. ");
return EXIT_FAILURE;
}
fprintf(fp, "static const char *m_pfuncnames[] = {\n");
for (i = 0; i < nfuncs; i++) {
fprintf(fp, "\"%s\",\n", funcs2[i]);
}
fprintf(fp, "};\n");
fprintf(fp, "static void *m_pfuncs[sizeof(m_pfuncnames) / sizeof(char*)];\n");
fprintf(fp, "\n");
for (i = 0; i < nfuncs; i++) {
fprintf(fp, "extern \"C\" __declspec(naked) void %s() { __asm jmp dword ptr [m_pfuncs + %d * 4] }\n", funcs2[i], i);
}
fprintf(fp, "\n");
fprintf(fp, "typedef void *VOIDPTR;\n");
fprintf(fp, "extern \"C\" {\n");
fprintf(fp, "VOIDPTR __stdcall LoadLibraryA(const char *lpFileName);\n");
fprintf(fp, "VOIDPTR __stdcall GetProcAddress(VOIDPTR hModule, const char *lpProcName);\n");
fprintf(fp, "int __stdcall MessageBoxA(VOIDPTR hWnd, const char *lpText, const char *lpCaption, unsigned int uType);\n");
fprintf(fp, "}\n");
p = strrchr(argv[1], '\\');
if (p) {
strcpy(dllfname, p + 1);
} else {
p = strrchr(argv[1], '/');
if (p) {
strcpy(dllfname, p + 1);
} else {
strcpy(dllfname, argv[1]);
}
}
fprintf(fp, "class Dummy {\n");
fprintf(fp, "public:\n");
fprintf(fp, "\tDummy() {\n");
fprintf(fp, "\t\tint i;\n");
fprintf(fp, "\t\tVOIDPTR hLibrary = LoadLibraryA(\"%s\");\n", dllfname);
fprintf(fp, "\t\tif (!hLibrary) {\n");
fprintf(fp, "\t\t\tMessageBoxA(0, \"%s がロードできません。\", 0, 0);\n", dllfname);
fprintf(fp, "\t\t\treturn;\n");
fprintf(fp, "\t\t}\n");
fprintf(fp, "\t\tfor (i = 0; i < sizeof(m_pfuncnames) / sizeof(char*); i++) {\n", nfuncs);
fprintf(fp, "\t\t\tm_pfuncs[i] = GetProcAddress(hLibrary, m_pfuncnames[i]);\n");
fprintf(fp, "\t\t}\n");
fprintf(fp, "\t}\n");
fprintf(fp, "};\n");
fprintf(fp, "static Dummy dummy;\n");
fprintf(fp, "\n");
fclose(fp);
strcpy(libfname, argv[1]);
p = strrchr(libfname, '.');
if (p) {
strcpy(p, ".lib");
} else {
strcat(libfname, ".lib");
}
system("cl /c __tmp__.cpp");
sprintf(cmd, "lib __tmp__.obj /out:%s", libfname);
system(cmd);
return EXIT_SUCCESS;
}
-----------------------------------------------------
|