[Solved]How to call OS api?

Help understanding and fixing compiling errors.
samsam598
Posts: 212
Joined: Thu Apr 14, 2011 9:44 pm

[Solved]How to call OS api?

Post by samsam598 »

Greetings!

As subjected,I tested on windows 7 with MessageBoxA function:

Code: Select all

 
import "ecere"
 
#ifdef win32
#include "windows.h"
#endif
 
class Form1 : Window
{
   text = "调用WIN32API";
   background = activeBorder;
   borderStyle = sizable;
   hasMaximize = true;
   hasMinimize = true;
   hasClose = true;
   size = { 440, 232 };
   anchor = { horz = -68, vert = -100 };
   //icon = { "C:\\person\\DevcppIcons\\dbnavedit.png", transparent = true };
 
   Button btnHello 
   {      
      this, text = "(M)欢迎", altM, size = { 90, 29 }, position = { 48, 152 };
 
      bool NotifyClicked(Button button, int x, int y, Modifiers mods)
      {  #ifdef win32
         MessageBoxA(0,"Caption","a message string",MB_OK|MB_ICONINFORMATION);
         #endif
         return true;
      }
   };
   Button btnQuit 
   {
      this, text = "(Q)退出", altQ, size = { 84, 29 }, position = { 312, 152 };
 
      bool NotifyClicked(Button button, int x, int y, Modifiers mods)
      {
         this.Destroy(0);
         return true;
      }
   };
}
 
Form1 form1 {};
 
 
But it seems that it does not work.Error message:

Code: Select all

form1.ec:1:1: error: Couldn't open obj/release.win32\form1.sym
Is it possible to do so in Ecere?Thanks.
Last edited by samsam598 on Tue Aug 23, 2011 8:24 pm, edited 1 time in total.
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: How to call OS api?

Post by jerome »

Hi Sam,

Sure, it's possible to do so. But why?
The goal of Ecere is to write cross-platform applications, so that kind of defeats the purpose.
Why don't you use the Ecere MessageBox instead?

MessageBox { text = "Caption", contents = "a message string" }.Modal();

The (cryptic) error you are getting is because preprocessor definitions should start on an empty line:

Add a new line before #ifdef win32 inside your code, it cannot follow any other character.

Also, the proper definition is __WIN32__, so you'll need to change that.

Because some of the code inside windows.h conflicts with Ecere data types, you need to put it at the top (before import "ecere") as well, and #define WIN32_LEAN_AND_MEAN to make sure it doesn't include extra stuff (which would require more workarounds to include):

Code: Select all

#ifdef __WIN32__
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: How to call OS api?

Post by jerome »

By the way I see you are trying to load up an icon in your program.
If you add the png file to the project resources folder, you can refer to it as ":dbnavedit.png" from within your program. (It will get included inside the executable)
samsam598
Posts: 212
Joined: Thu Apr 14, 2011 9:44 pm

Re: How to call OS api?

Post by samsam598 »

Thanks Jerome.MessageBox just win32 api that I can easily remember.Yes MessageBox in Ecere is more user friendly than the OS api.The concern is just for two purpose:one is to ensure Ecere is 100% compatible with C;two is calling OS api is not a problem in Ecere.Sometimes programmer can't avoid to interact with OS api,for example,what if I want to manipulate an excel file together with SQL.

After my modifying the little program with your guidline,the program can run now,but with a little problems:
1.Button btnHello hotkey ALT+M can not work properly,it will minimize the program;
2.The Chinese characters in the MessageBox can't be recognized.All a mess.Trying to use win32 Macro _T and or L can't get compiled any more.

Regards,
Sam
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: How to call OS api?

Post by jerome »

Hi Sam, Yes I understand that's what you're trying to do with MessageBox :)

I realize Alt-M is a bit drastic for the Minimize button hehe.
We'll have to review if we want to change that. In the meantime here is a workaround to put in your Form1 class:

Code: Select all

   bool OnPostCreate()
   {
      minimizeButton.hotKey = 0;
      return true;
   }
About Chinese text with the Windows API, eC files are typically encoded using UTF-8, while Windows APIs uses UTF-16. Also you will need to #define UNICODE at the top for MessageBox, or use MessageBoxW. Here's some code that works:

Code: Select all

         uint16 * s = UTF8toUTF16("你好", null);
         MessageBoxW(0,s,s,MB_OK|MB_ICONINFORMATION);
         delete s;
There are 3 varieties of the UTF8toUTF16 function, depending on how you want to manage the memory.
samsam598
Posts: 212
Joined: Thu Apr 14, 2011 9:44 pm

Re: How to call OS api?

Post by samsam598 »

Thanks,although #define UNICODE does not make sense,function UTF8toUTF16 solved the problem perfectly.
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: How to call OS api?

Post by jerome »

When you #define UNICODE before including windows.h, it will map the macro MessageBox to MessageBoxW (And all other Windows APIs this way as wel). It's up to you if you want to explicitly say e.g. MessageBoxA or MessageBoxW everywhere in your code (then you don't need to #define UNICODE).

NOTE: You will hit another name conflict with Ecere's MessageBox class, so might as well use MessageBoxW if you don't want to redefine things :P
samsam598
Posts: 212
Joined: Thu Apr 14, 2011 9:44 pm

Re: How to call OS api?

Post by samsam598 »

Sorry ,I did as you mentioned,but the way #define UNICODE can't make the Chinese characters be recognizable still.

Resource ":iconfile.png" will be compiled and included in the executable file is great!I like it! I noticed there is only icon in the task bar,not in the top left of the form.
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: How to call OS api?

Post by jerome »

With #define UNICODE , you have the MessageBox conflict. And you still need to use UTF8to16...

It doesn't change that. It just defines a MessageBox macro mapping to MessageBoxW, so you can type MessageBox instead of MessageBoxW. But you have conflict with Ecere's MessageBox, so it's useless in that case, but useful if you use many different Windows APIs like that, and useful for people who want to switch between supporting international languages and just ASCII text.

Ecere itself does not display icons on the title bar when it draws window decorations. If you use the latest ecere and set 'nativeDecorations = true' for your form, then you should get the icon displaying on the title bar.

Also, if you want an icon for your executable file, you need to build a small .rc file, save the icon as an .ico file, compile the .rc file with windres (e.g. windres resources.rc resources.o), and add in Project Settings / Linking, under 'Additional Libraries', 'resources.o' to link in your icon. A sample resources.rc would contain:

Code: Select all

IDI_ICON               ICON    DISCARDABLE     "icon.ico"
samsam598
Posts: 212
Joined: Thu Apr 14, 2011 9:44 pm

Re: How to call OS api?

Post by samsam598 »

For Icon just tested,if I assign an icon file to the form,the nativeDecorations cann't be set any more(If I set this property to true prior to assian the icon file,the nativeDocorations will turn back to false).I got a crash--one time; the nativeDecorations turned back to false--seems always.
Post Reply