• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

How to mimic GOSUB in C ?

Started by Patrice Terrier, February 12, 2013, 06:03:44 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Frederick J. Harris

Here's an interesting little tidbit of info James might be interested in ...

... to time the compilation speed of the mingw compiler, use this switch in your compiler string ...

-ftime-report

It produces a report.

I've found the older compiler with my Code::Blocks installation is much faster than the newer one I downloaded a month ago.  Also, the PowerBASIC compiler has really slowed down.  I guess the dead code removal.

Patrice Terrier

I know you like the Microsoft C/C++ compiler with VS
The reason why i use Visual Studio, is that it embraces all the Microsoft technologies within a single package, including managed and un-managed code.

So far, if C# would have been able to produce un-managed code, that would have been my compiler of choice. But i don't like the use of the extra frame-work, and the garbage collector manager. Non-obstant the fact, that i am writing mostly "unsafe" code ;)

Thus i put my choice on C/C++ without ATL and MFC, because i want to produce code without extra runtime, and i want to have direct control on what is going on under the hood, but i really hate the C/C++ syntax and the strong type they are using, HWND, HBITMAP, HANDLE, etc.,  when all could be replaced by DWORD. Also i never know exactly where to put the "*" when declaring/working with pointers.

Translating my GDImage to produce a 64-bit version is a very slow process, because i have to learn all the C/C++ oddities at the same time  :(

...

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

Charles Pegge

Patrice,

Are you now prepared to submit to the anti-gosub mind clamp? :)

The only other sensible way is to OOPify your code so that all member variables can be shared between member functions (methods).

Frederick J. Harris

Quote
The only other sensible way is to OOPify your code so that all member variables can be shared between member functions (methods).

That's the beauty of inlining class members; it saves the overhead of a seperate function call, which, of course, is what gosub does.

I'll tell you what Patrice, I've had trouble over the years between the slightly different pointer notation between PowerBASIC and C.  They are so similiar, but yet there are subtle differences that drives one crazy.  I do have various code snippets handy outlining the differences perhaps you would be interested in.  Because I use pointers so heavily in my work, I've spent countless hours playing around with these things. 

Patrice Terrier

#19
QuoteAre you now prepared to submit to the anti-gosub mind clamp?
No, i will go the evil's way using couples of jmp/GOTO, because i am lazy and i want to keep the C code, close to the PB's original.

Did i say i like spaghetti  ;D

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

Patrice Terrier

#20
Here is an example of C code that drives me nut.

Code in red causes:
Error: a pointer bound to a function may only be used to call a function

typical syntax problem i am faced with...

Quote#include <windows.h>
#include "stdafx.h"

#include <vector>
#include <string.h>
using namespace std;


wstring zGetCTLText(IN HWND hCtrl) {
    std::wstring sBuf;
    long bufsize = 4096;
    vector<WCHAR>buf(bufsize);
    long length = min(GetWindowText(hCtrl, &buf[0], bufsize), bufsize);
    return wstring(buf.begin(), buf.begin() + length);
}

int _tmain(int argc, _TCHAR* argv[])
{
    HWND hWnd = GetForegroundWindow();
    wstring sCaption = zGetCTLText(hWnd);
    if (sCaption.length) { printf("%s", sCaption); }
    return 0;
}

And you know what, the problem is because the ".length" member must be followed by two empty (), like this:
if (sCaption.length()) { printf("%s", sCaption); }

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

Paul Squires

Patrice, I've encounter the same problem while learning C#....forgetting to include the trailing open and close parenthesis when calling a method. Once I made the mistake and figured out the solution then I've been okay ever since. Coming from a VisualBasic background, we never had to include the parenthesis. I guess that "C" languages are more strict.
Paul Squires
FireFly Visual Designer SQLitening Database System JellyFish Pro Editor
http://www.planetsquires.com

Patrice Terrier

Paul

The C# syntax is much more easier to read/write from my opinion, but it is managed code  ???

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

Frederick J. Harris

#23
That doesn't bother me about needing the brackets () on member function calls.  I'm more bothered with languages that don't require it (such as vb I believe - PB too?), as it seems to overlook the distinction between ordinary variables and member functions.

Patrice, what does this do


vector<WCHAR>buf(bufsize);


Create a vector whose upper limit of growth is restricted to bufsize, as opposed to the standard behaviour of unlimited growth? 

Anyway, how is your function to get text from a control any better than this (short compilable example)...


//Main.cpp - Developed with Code::Blocks / mingw; compiles to 57 k for me with 10.05 / 95 k with 12.11.
#define  UNICODE
#define  _UNICODE
#include <windows.h>
#include <tchar.h>
#include <string>
#define  IDC_BUTTON 1600


std::wstring strGetText(HWND hCtrl)
{
TCHAR szBuffer[4096];
GetWindowText(hCtrl,szBuffer,4096);
return std::wstring(szBuffer);
}


LRESULT CALLBACK fnWndProc(HWND hwnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
  case WM_CREATE:
    {
       HINSTANCE hIns=((LPCREATESTRUCT)lParam)->hInstance;
       CreateWindowEx(0,_T("button"),_T("Click Me!"),WS_CHILD|WS_VISIBLE,75,50,150,30,hwnd,(HMENU)IDC_BUTTON,hIns,0);
       return 0;
    }
  case WM_COMMAND:
    {
       if(LOWORD(wParam)==IDC_BUTTON)
       {
          HWND hMain=GetForegroundWindow();
          MessageBox(hwnd,strGetText(hMain).c_str(),_T("Your Text..."),MB_OK);
       }
       return 0;
    }
  case WM_DESTROY:
    {
       PostQuitMessage(0);
       return 0;
    }
}

return (DefWindowProc(hwnd, msg, wParam, lParam));
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevIns, LPSTR lpszArgument, int iShow)
{
TCHAR szClassName[]=_T("Text Wrapper Demo");
WNDCLASSEX wc={};
MSG messages;
HWND hWnd;

wc.lpszClassName = szClassName;
wc.lpfnWndProc   = fnWndProc;
wc.cbSize        = sizeof(WNDCLASSEX);
wc.hbrBackground = (HBRUSH)COLOR_BTNSHADOW;
wc.hInstance     = hInstance;
RegisterClassEx(&wc);
hWnd=CreateWindowEx(0,szClassName,szClassName,WS_OVERLAPPEDWINDOW,200,175,320,200,HWND_DESKTOP,0,hInstance,0);
ShowWindow(hWnd,iShow);
while(GetMessage(&messages,NULL,0,0))
{
    TranslateMessage(&messages);
    DispatchMessage(&messages);
}

return messages.wParam;
}

Patrice Terrier

#24
Quotehow is your function to get text from a control any better than this
that was learning code to check the use of VECTOR (to mimic dynamic array), not production code.

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

Frederick J. Harris

Quote
that was learning code to check the use of VECTOR (to mimic dynamic array), not production code.

Oh! :)

I'm such a minimalist by nature, once I understand something that looks overly complicated, I always start to thinking, "Boy!  I bet we could get rid of this or that or whatever!"