Thread overview
Error execute MS function fputc()
Jun 07, 2015
MGW
Jun 08, 2015
MGW
June 07, 2015
Hi, everybody!

DMD32 D Compiler v2.067.0
I try to cause the fputc function from CrtDLL.DLL and I receive a mistake. Why?

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");
	
	import core.stdc.stdio: stdout;
	try {
		auto rez = ms_fputc('A' , core.stdc.stdio.stdout);
    }
	catch {
		writeln("Error execute MS function fputc();");
	}
    return 0;
}
June 08, 2015
On 6/7/15 12:41 PM, MGW wrote:
> Hi, everybody!
>
> DMD32 D Compiler v2.067.0
> I try to cause the fputc function from CrtDLL.DLL and I receive a
> mistake. Why?
>
> 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");
>
>      import core.stdc.stdio: stdout;
>      try {
>          auto rez = ms_fputc('A' , core.stdc.stdio.stdout);
>      }
>      catch {
>          writeln("Error execute MS function fputc();");
>      }
>      return 0;
> }

DMD 32-bit does not use MSVC runtime, it uses DMC (Digital Mars C++ Compiler) runtime. So you cannot use MSVC versions of C runtime.

You can use Win32 system calls, though.

-Steve
June 08, 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;
}