Error execute MS function fputc()
MGW via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Jun 8 11:14:03 PDT 2015
I found the solution. A problem in different structures of FILE
for MSVC and DMC. I take the pointer on structure of FILE from
CrtDLL.dll
Everything works!
// MGW 07.05.15
import core.runtime; // Загрузка DLL Для Win
import std.stdio; // writeln
version(Windows) {
import std.c.windows.windows; // GetProcAddress для Windows
}
alias extern (C) int function(int, FILE*) t_fputc;
t_fputc ms_fputc;
int main(string[] args) {
version(Windows) { auto libCrtDll = "CrtDll.dll"; }
// Load CrtDLL.DLL MS Windows
auto h = Runtime.loadLibrary(libCrtDll);
// Find function fputc
ms_fputc = cast(t_fputc)GetProcAddress(h, "fputc");
// Find array struct FILE MS
void* ubuf = cast(void*)GetProcAddress(h, "_iob");
import core.stdc.stdio: stdout;
try {
// stdout --> FILE dmc
auto rez = ms_fputc('A' , core.stdc.stdio.stdout);
}
catch {
writeln("Error execute MS function fputc();");
}
try {
// cast(FILE*)(ubuf + 0) ==> stdin MSVC
// cast(FILE*)(ubuf + 32) ==> stdout MSVC
// stdout --> FILE MSVC
auto rez = ms_fputc('B' , cast(FILE*)(ubuf + 32));
}
catch {
writeln("Error execute MS function ms_fputc();");
}
return 0;
}
More information about the Digitalmars-d-learn
mailing list