• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

My first steps with C++

Started by Patrice Terrier, July 30, 2010, 10:01:29 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Patrice Terrier

I started this new thread to see how close i could convert some of my PB's code into C++ using the core SDK syntax i am familiar with.

...
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Patrice Terrier

#1
This is a bare bone Win32 SDK window template:

// HelloWin32.cpp
// compile with: /D_UNICODE /DUNICODE /DWIN32 /D_WINDOWS /c

#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>

// Global variable
HINSTANCE g_hInst; // Current instance

// Declaration of the functions being used in this module:
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
//INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);

int WINAPI wWinMain(HINSTANCE hInstance,
                    HINSTANCE hPrevInstance,
                    LPTSTR lpCmdLine,
                    int nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // Declaration of local variables
    MSG msg;
    int nRet = 0;
    int IsInitialized = 0;
    WNDCLASSEX wcx;
    TCHAR szWindowClass[] = _T("win32app");
    TCHAR szTitle[] = _T("Win32 Guided Tour Application");

    // Register the main window class
    wcx.cbSize = sizeof(WNDCLASSEX);
    IsInitialized = GetClassInfoEx(hInstance, szWindowClass, &wcx);
    if (!IsInitialized)
    {
        wcx.style         = CS_HREDRAW | CS_VREDRAW;
        wcx.lpfnWndProc   = WndProc;
        wcx.cbClsExtra    = 0;
        wcx.cbWndExtra    = 0;
        wcx.hInstance     = hInstance;
        wcx.hIcon         = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
        wcx.hCursor       = LoadCursor(NULL, IDC_ARROW);
        wcx.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
        wcx.lpszMenuName  = NULL;
        wcx.lpszClassName = szWindowClass;
        wcx.hIconSm       = LoadIcon(wcx.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
        if (RegisterClassEx(&wcx))
        {
           IsInitialized = -1;
        }
     }

     if (IsInitialized)
     {
        g_hInst = hInstance; // Stocke le handle d'instance dans la variable globale
   
        HWND hWnd = CreateWindowEx(
                    0,                              // Optional window styles.
                    szWindowClass,                  // Window class
                    szTitle,                        // Window text
                    WS_OVERLAPPEDWINDOW,            // Window style
   
                    // Size and position
                    CW_USEDEFAULT, CW_USEDEFAULT, 640, 480,
   
                    NULL,       // Parent window   
                    NULL,       // Menu
                    hInstance,  // Instance handle
                    NULL        // Additional application data
                    );
   
        if (hWnd)
        {
   
           ShowWindow(hWnd, nCmdShow);
           UpdateWindow(hWnd);
   
           // Main message loop
           while (GetMessage(&msg, NULL, 0, 0))
           {
               TranslateMessage(&msg);
               DispatchMessage(&msg);
           }
           nRet = msg.wParam;
        }
     }
   
     return nRet;
}

// Main winproc
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;

    switch (uMsg)
    {
    case WM_COMMAND:
         wmId    = LOWORD(wParam);
         wmEvent = HIWORD(wParam);
         // Process menu command
         //switch (wmId)
         //{
         //case IDM_ABOUT:
         //     DialogBox(g_hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
         //     break;
         //case IDM_EXIT:
         //     DestroyWindow(hWnd);
         //     break;
         //}
         return 0;
    case WM_PAINT:
         hdc = BeginPaint(hWnd, &ps);
         // TODO: process here the WM_PAINT message...
         EndPaint(hWnd, &ps);
         return 0;
    case WM_DESTROY:
         PostQuitMessage(0);
         return 0;
    }
   
    return DefWindowProc(hWnd, uMsg, wParam, lParam);


//// The about dialog function
//INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
//{
//    UNREFERENCED_PARAMETER(lParam);
//    switch (message)
//    {
//    case WM_INITDIALOG:
//         return (INT_PTR)TRUE;
//
//    case WM_COMMAND:
//         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
//         {
//            EndDialog(hDlg, LOWORD(wParam));
//            return (INT_PTR)TRUE;
//         }
//         break;
//    }
//    return (INT_PTR)FALSE;
//}



Note: This bare bone window is compatible 32/64-bit, because UINT or INT are being used only when it it is the same in 32/64-bit.
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Jürgen Huhn

Looks good Patrice,

and also compatible 32/64bit!

Here my procedures for an OpenGl Window:
void Shutdown()
{
if (fullscreen)
{
ChangeDisplaySettings(NULL,0);
ShowCursor(TRUE);
}

wglMakeCurrent(NULL,NULL);

wglDeleteContext(RC);
RC=NULL;

ReleaseDC(hWnd,DC);
DC=NULL;

DestroyWindow(hWnd);
hWnd=NULL;

UnregisterClass(ClassName, hInstance);
hInstance=NULL;
}


int LoadTexture( unsigned &Texturehandle , char * texturefilename)
{

pngInfo info;

glGenTextures(1, &Texturehandle);
glBindTexture(GL_TEXTURE_2D, Texturehandle);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

if (pngLoad(texturefilename, PNG_BUILDMIPMAPS, PNG_ALPHA, &info)) {
   
return TRUE;

}else{

return FALSE;

}

}


int InitTextures()
{
        // Code
return TRUE;

}

int InitGL()
{

glEnable(GL_TEXTURE_2D);

if (!InitTextures())
return FALSE;

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

glEnable(GL_BLEND);

glBlendFunc(GL_SRC_ALPHA,GL_DST_ALPHA);

//glDisable(GL_DEPTH_TEST);
//glClearDepth(1.0);
//glDepthFunc(GL_LESS);
//glEnable(GL_DEPTH_TEST);

glShadeModel(GL_SMOOTH);

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

return TRUE;
}


// ReSize
void ReSize(int width, int height)
{
glViewport(0,0,width,height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective (45,(double)width/(double)height,1,100000);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}


LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam,LPARAM lParam)
{
switch (uMsg)
{
case WM_SYSCOMMAND:
{
switch (wParam)
{
case SC_SCREENSAVE:
case SC_MONITORPOWER:
return 0;
}
break;
}

case WM_CLOSE:
{
b_actif=FALSE;
return 0;
}

case WM_KEYDOWN:
{
// Code
return 0;
}

case WM_KEYUP:
{
// Code
return 0;
}

case WM_SIZE:
{
ReSize(LOWORD(lParam),HIWORD(lParam));
return 0;
}
}

return DefWindowProc(hWnd,uMsg,wParam,lParam);
}


// CreateGLWindow
BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag)
{
WNDCLASS wc;
DWORD dwExStyle;
DWORD dwStyle;
RECT WindowRect;
WindowRect.left=(long)0;
WindowRect.right=(long)width;
WindowRect.top=(long)0;
WindowRect.bottom=(long)height;
fullscreen=fullscreenflag;

hInstance = GetModuleHandle(NULL);
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = ClassName;

RegisterClass(&wc);

if (fullscreen)
{
DEVMODE dmScreenSettings;
memset(&dmScreenSettings,0,sizeof(dmScreenSettings));
dmScreenSettings.dmSize=sizeof(dmScreenSettings);
dmScreenSettings.dmPelsWidth = width;
dmScreenSettings.dmPelsHeight = height;
dmScreenSettings.dmBitsPerPel = bits;
dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;

ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN);
}

if (fullscreen)
{
dwExStyle=WS_EX_APPWINDOW;
dwStyle=WS_POPUP;
}
else
{
dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
dwStyle=WS_OVERLAPPEDWINDOW;
}

ShowCursor(TRUE);

AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);

hWnd=CreateWindowEx(dwExStyle, ClassName, title, dwStyle | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
0, 0, WindowRect.right-WindowRect.left, WindowRect.bottom-WindowRect.top,
NULL, NULL, hInstance, NULL);

static PIXELFORMATDESCRIPTOR pfd=
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
bits,
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
32,
0,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0
};

DC=GetDC(hWnd);

SetPixelFormat(DC,ChoosePixelFormat(DC,&pfd),&pfd);

RC=wglCreateContext(DC);

wglMakeCurrent(DC,RC);

ShowWindow(hWnd,SW_SHOW);
SetForegroundWindow(hWnd);
SetFocus(hWnd);

return TRUE;
}



int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;

// on crée la fenêtre
if(!CreateGLWindow(APPName,1024,768,32,fullscreen))
{
MessageBox (HWND_DESKTOP, "Impossible de créer la fenêtre principale", "Erreur", MB_OK | MB_ICONEXCLAMATION);

// on quitte le programme
return -1;

}


// on initialise le programme
if(!InitGL())
{
MessageBox (HWND_DESKTOP, "Impossible d'initialiser le programme", "Erreur", MB_OK | MB_ICONEXCLAMATION);

// on quitte le programme
return -1;

}

        // Rezize routine
ReSize(1024, 768);

b_actif = TRUE;

while(b_actif)
{

if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{

if (msg.message==WM_QUIT)
{

b_actif=FALSE;
}
else
{
// non, on passe le message
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else
{

RePaint();

}
}


Shutdown();

// on quitte le programme
return (msg.wParam);
}


Maybe it`s also usedull!?

..  ;)
.¸.•'´¯)¸.•'´¯)¸.•'´¯)¸.•'´¯)
¤ª"˜¨¨¯¯¨¨˜"ª¤....¤ ª"˜¨

Patrice Terrier

Jürgen,

Indeed my question was:
Quotehow to create the C++ header file for my GDImage.inc

Here is an example of what i would like to turn into a C++ header:

'//5.06
' Assign a new texture to an existing OpenGL "Named texture".
  DECLARE FUNCTION ZI_UpdateNamedGLTextureFromFileEx LIB "GDIMAGE.DLL" ALIAS "ZI_UpdateNamedGLTextureFromFileEx" ( _
  zFullName AS ASCIIZ, _        ' Full qualified path name.
  BYVAL NamedTexture AS LONG, _ ' An existing OpenGL NamedTexture
  BYVAL SquareMode AS LONG _    ' The square mode to use
  ) AS LONG
' Return:
' An OpenGL error code in case of Error.


I know how i could do it using "LoadDll" and "LoadLibrary" (explicit linking)
but i don't know the syntax to use to make implicit linking like in PowerBASIC.
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Jürgen Huhn

 ::)

Sorry Patrice,
for having misunderstood your Question:

I was on another Planet and on the way to thinking about that`s needed to
use more and more C++/C# next comming times.
It makes not really Sense to Compile for WinVista and Win7 in 32bit Code and Compiler, because after last updares for InternetExplorer8 and OperatingSystems some functions doesn´t work.
I use an add on Menu, that`s a little Program that`s starts with IExplorer. I wrote it in PB 32bit.
The Menü appears but some calls on Functions from Menu to IExplorer8 are in 64bit wide on new Datatype`s or something like else.
Instead to lost a lot time by figure out what it is, i  translatet the PB code and wrote a small part newi in C++.  After short time debugging and compiled with Microsoft Visual Basic 2010...
The Menu appears faster and also a bit smoother or better rendered on open and closing time.
But i changed no Datatypes off my MenuCode to 64bit. I thought just to try it and all works very well.

I hope a PB 64bit Compiler is comming soon!

QuoteI know how i could do it using "LoadDll" and "LoadLibrary" (explicit linking)
but i don't know the syntax to use to make implicit linking like in PowerBASIC.

Of course Patrice and i know this very well.. Instead of understanding your Question i wonder!!Also about your first steps..


??? ???

:)

Sorry once again,

...
.¸.•'´¯)¸.•'´¯)¸.•'´¯)¸.•'´¯)
¤ª"˜¨¨¯¯¨¨˜"ª¤....¤ ª"˜¨

Patrice Terrier

C++ gurus, my question is still there.

What is the syntax to use to call from a C++ Win32 application, a function or subroutine exported from a PowerBASIC 32-bit DLL?

:o
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Patrice Terrier

#6
Here is an example of explicit DLL linking, using LoadLibrary and GetProcAdress to call my zTrace utility.

void zTrace(char szMsg[])
{
   typedef int (__stdcall *ZTRACE) (char*);
   ZTRACE hPROC;

   HMODULE hDLL = LoadLibrary(_T("zTrace.dll"));
   if (hDLL)
   {
      hPROC = (ZTRACE) GetProcAddress(hDLL, "zTrace");
      if (hPROC)
      {
          int nRet = hPROC(szMsg);
      }
      else
      {
          FreeLibrary(hDLL);
      }
   }
}


Now how to use it with implicit linking?

...
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

José Roca

Quote
To implicitly link to a DLL, executables must obtain the following from the provider of the DLL:

    * A header file (.h file) containing the declarations of the exported functions and/or C++ classes. The classes, functions, and data should all have __declspec(dllimport), for more information, see dllexport, dllimport.

    * An import library (.LIB files) to link with. (The linker creates the import library when the DLL is built.)

    * The actual DLL (.dll file).

http://msdn.microsoft.com/en-us/library/d14wsce5%28VS.90%29.aspx

Guess that the C++ package will include a utility to make an import .lib file from the .dll.

Patrice Terrier

#8
José,

QuoteTo implicitly link to a DLL, executables must obtain the following from the provider of the DLL:
   * A header file (.h file) containing the declarations of the exported functions and/or C++ classes. The classes, functions, and data should all have __declspec(dllimport), for more information, see dllexport, dllimport.
   * An import library (.LIB files) to link with. (The linker creates the import library when the DLL is built.)
   * The actual DLL (.dll file).

I have read the help file, and i see this already...
But the problem is how to create the LIB file.  ;)

Hello Mike?

Added: Looks like the solution would be to use LIB.exe

Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

James C. Fuller

And now you have to create four libraries instead of just one with PB.
ansi 32 bit
ansi 64 bit
unicode 32 bit
unocode 64 bit

James

Patrice Terrier

So far ANSI 32-bit would be fine  ;D

...

Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

James C. Fuller


Patrice Terrier

#12
James,

Thank you.

Here is another link:
http://support.microsoft.com/kb/131313/en-us

I never thought that it was so hard to use an external DLL written with another language with C++

in C# it is almost as easy as in PB, example:
       [DllImport(GDIMAGE)]  // Retrieve the GDImage mouse captured X,Y location.
       public static extern void ZD_GetObjectXYcapture(
       int ObjID,          // The unique sprite object identifier.
       out int x,          // The X coordinate
       out int y           // The Y coordinate
       );

Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Jürgen Huhn

#13
I wanted to give you a sign as long PB is compiling in 32bit, translate the DLL to c++ is the best.

Or go to:
http://msdn.microsoft.com/en-us/library/7ykb2k5f(v=VS.90).aspx

Here is a description with code examples
http://www.codeproject.com/KB/DLL/loadingdll.aspx
.¸.•'´¯)¸.•'´¯)¸.•'´¯)¸.•'´¯)
¤ª"˜¨¨¯¯¨¨˜"ª¤....¤ ª"˜¨

James C. Fuller

Patrice,
  Even if you are committed to Visual c++   It might help to play around with Bcx3264. Write the code in Bcx Then examine the c++ code created. I know it has helped me quite bit with my c++ education.

James