www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Error execute MS function fputc()

reply "MGW" <mgw yandex.ru> writes:
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;
}
Jun 07 2015
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
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
Jun 08 2015
parent "MGW" <mgw yandex.ru> writes:
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;
}
Jun 08 2015