www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - I try import windows API CharToOem function

reply "Quentin J." <quentin.joseph outlook.com> writes:
Hello, (sorry for my english, i'm french ^^)

I try to implement CharToOem function in my code for use accent 
in windows console but she doesn't work, i have one error from 
xamarin studio : "bool.main.CharToOem(const char*, char*) could 
not be resolved - librairy reference missing" :'(.

CharToOem Function :

BOOL WINAPI CharToOem(
   _In_   LPCTSTR lpszSrc,
   _Out_  LPSTR lpszDst
);

LPCTSTR = const char* or const wchar_t* depending on _UNICODE.
LPSTR = char*

My code is :

import std.c.windows.windows;
extern (Windows) bool CharToOem(const char*, char*);

public void PutStringIntoConsole(string text)
{
	char[] source = text.dup;
	char[] dest;

	CharToOem(source.ptr, dest.ptr);
	writeln(dest);
}

Can you help me please, i'm novice in D.
Aug 26 2013
next sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Monday, 26 August 2013 at 17:42:49 UTC, Quentin J. wrote:
 extern (Windows) bool CharToOem(const char*, char*);

Try: // use the unicode version of the Windows function extern (Windows) bool CharToOemW(const wchar*, char*); alias CharToOemW CharToOem; Windows functions often have an A or a W at the end of them, for ascii or wide char version. The name without A or W is just an alias or #define for many functions. Your function is wrong too:
 	char[] source = text.dup;

This might not zero terminate the string, and won't be right for Unicode characters. Better to do: import std.utf; auto source = toUTF16z(text);
 	char[] dest;

 	CharToOem(source.ptr, dest.ptr);

And this will give you an AccessViolation if you run it because dest is null. // allocate some space for the new string first char[] dest = new char[](text.length * 2); To to bring it together: import std.c.windows.windows; // use the unicode version of the Windows function extern (Windows) bool CharToOemW(const wchar*, char*); alias CharToOemW CharToOem; public void PutStringIntoConsole(string text) { // convert source into a Windows tchar* string import std.utf; auto source = toUTF16z(text); // prepare our buffer with enough space to receive the data char[] dest = new char[](text.length * 2); // call the function... CharToOem(source, dest.ptr); // we also want to get the length out instead of relying on zero termination for a real D string: import core.stdc.string; dest = dest[0 .. strlen(dest.ptr)]; writeln(dest); } That should give you better results.
Aug 26 2013
prev sibling parent "Quentin J." <quentin.joseph outlook.com> writes:
On Monday, 26 August 2013 at 18:02:51 UTC, Adam D. Ruppe wrote:
 To to bring it together:

 import std.c.windows.windows;

 // use the unicode version of the Windows function
 extern (Windows) bool CharToOemW(const wchar*, char*);
 alias CharToOemW CharToOem;


 public void PutStringIntoConsole(string text)
 {
         // convert source into a Windows tchar* string
         import std.utf;
         auto source = toUTF16z(text);

         // prepare our buffer with enough space to receive the 
 data
         char[] dest = new char[](text.length * 2);

         // call the function...
         CharToOem(source, dest.ptr);

         // we also want to get the length out instead of 
 relying on zero termination for a real D string:
         import core.stdc.string;
         dest = dest[0 .. strlen(dest.ptr)];

         writeln(dest);
 }

It works,
 	char[] dest;

	CharToOem(source.ptr, dest.ptr);


And this will give you an AccessViolation if you run it because 
dest is null.

For the array, it is my fault an error of inattention ^^ Thank for your explanation and your help. Quentin.
Aug 26 2013