NO

Author Topic: Compiler trouble  (Read 8967 times)

Zodra1

  • Guest
Compiler trouble
« on: July 14, 2008, 04:33:09 PM »
I don't know what is wrong with me. After testing SQL ODBC connect I started to develop main application. I generated MDI app. with wizard. Iz compiled well until i added SQLAllocHandle. Below is code:

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <windowsx.h>
#include <winuser.h>
#include <sql.h>
#include <sqlext.h>

#include <stdio.h>

#include "main.h"
#include "statusbar.h"
#include "toolbar.h"

HINSTANCE g_hInst;   // instance handle
HWND g_hWndFrame;   // main window handle
HWND g_hWndClient;   // mdi client window handle
HWND g_hWndToolBar;   // toolbar window handle
HWND g_hWndStatusBar;   // statusbar window handle
HBITMAP g_hBmp;   // toolbar button's bitmap

int numParts = 4; // number of sections for statusbar
// ODBC connection to Mafips30 database *************************************************************************
static void extract_error(
    char *fn,
    SQLHANDLE handle,
    SQLSMALLINT type);

    SQLHENV env;
    SQLHDBC dbc;
    SQLHSTMT stmt;
    SQLRETURN ret; /* ODBC API return status */
    SQLCHAR outstr[1024];
    SQLSMALLINT outstrlen;

  /* Allocate an environment handle */

SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);

// End od ODBC connect **********************************************************************************************

This line produced following errors:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <windowsx.h>
#include <winuser.h>
#include <sql.h>
#include <sqlext.h>

#include <stdio.h>

#include "main.h"
#include "statusbar.h"
#include "toolbar.h"

HINSTANCE g_hInst;   // instance handle
HWND g_hWndFrame;   // main window handle
HWND g_hWndClient;   // mdi client window handle
HWND g_hWndToolBar;   // toolbar window handle
HWND g_hWndStatusBar;   // statusbar window handle
HBITMAP g_hBmp;   // toolbar button's bitmap

int numParts = 4; // number of sections for statusbar
// ODBC connection to Mafips30 database *************************************************************************
static void extract_error(
    char *fn,
    SQLHANDLE handle,
    SQLSMALLINT type);

    SQLHENV env;
    SQLHDBC dbc;
    SQLHSTMT stmt;
    SQLRETURN ret; /* ODBC API return status */
    SQLCHAR outstr[1024];
    SQLSMALLINT outstrlen;

  /* Allocate an environment handle */

SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);

// End od ODBC connect **********************************************************************************************
This line produced three errors (line 48)

D:\Pelles_Razvoj\MaFiP'S 3.0\mafips30\main.c(48): warning #2099: Missing type specifier.
D:\Pelles_Razvoj\MaFiP'S 3.0\mafips30\main.c(48): error #2001: Syntax error: expected ')' but found 'integer constant'.
D:\Pelles_Razvoj\MaFiP'S 3.0\mafips30\main.c(48): error #2120: Redeclaration of 'SQLAllocHandle' previously declared at C:\Program Files\PellesC\Include\Win\sql.h(433): found 'int __cdecl function()', expected 'short int __stdcall function(short int, void *, void * *)'.
D:\Pelles_Razvoj\MaFiP'S 3.0\mafips30\main.c(147): warning #2168: Operands of = have incompatible types 'int __stdcall function(HWND, long int)' and 'int __cdecl function(HWND, long int)'.
D:\Pelles_Razvoj\MaFiP'S 3.0\mafips30\main.c(534): warning #2030: = used in a conditional expression.
*** Error code: 1 ***

It would also be very helpful if someone could explain last two warning messages.



JohnF

  • Guest
Re: Compiler trouble
« Reply #1 on: July 14, 2008, 10:00:49 PM »
static void extract_error(
    char *fn,
    SQLHANDLE handle,
    SQLSMALLINT type);

The above is not correct - see if you can tell what's wrong. When you've corrected it see if it compiles better.

And this:
D:\Pelles_Razvoj\MaFiP'S 3.0\mafips30\main.c(534): warning #2030: = used in a conditional expression.

   if(i = 0) // this is most often a mistake so the compiler warns about it.

You probably want if(i == 0)

And this;
D:\Pelles_Razvoj\MaFiP'S 3.0\mafips30\main.c(147): warning #2168: Operands of = have incompatible types 'int __stdcall function(HWND, long int)' and 'int __cdecl function(HWND, long int)'.

There is a function for which the calling convention is not correct. I can't see the code, make sure that it is prefixed with the right one. Probably __cdecl

e.g.
int __cdecl myfunc(HWND h, long t)

John

Zodra1

  • Guest
Re: Compiler trouble
« Reply #2 on: July 15, 2008, 11:11:02 AM »
static void extract_error(
    char *fn,
    SQLHANDLE handle,
    SQLSMALLINT type);
Must admit, that I am too inexperienced in C to figure out what is wrong with function declaration. Not that i haven't tried.

Function call is:
EnumChildWindows(g_hWndClient, EnumChildDestroy, 0);

This was generated by MDI wizard; definition of EnumChildDestroy:
int EnumChildDestroy(HWND hwnd, LPARAM lParam);
and call:
EnumChildWindows(g_hWndClient, EnumChildDestroy, 0);

Can't find EnumChildWindows in the code; possible API call?

I will keep trying of course; but help is more than welcome.

Zodra1

  • Guest
Re: Compiler trouble
« Reply #3 on: July 15, 2008, 11:22:16 AM »
Function is API, i found declaration in winuser.h:
WINUSERAPI BOOL WINAPI EnumChildWindows(HWND,WNDENUMPROC,LPARAM);
It is BOOL-ean type, must be that those should be called differently?

JohnF

  • Guest
Re: Compiler trouble
« Reply #4 on: July 15, 2008, 05:47:49 PM »
EnumChildWindows() API is __stdcall. Are you compiling a Console app?

Console apps default to __cdecl.

Post the code. edit: If you are not bashful.

John
« Last Edit: July 16, 2008, 10:35:37 AM by JohnF »

Zodra1

  • Guest
Re: Compiler trouble
« Reply #5 on: July 16, 2008, 11:36:37 AM »
I am not in any way embarrassed. Better to ask about what you don't know than to be stupid all your life :) The code is attached. It is simply code generated by MDI wizard and then i added code for ODBC connect.

Still don't know what is wrong with declaration of extract_error. All this statements can be found in this link: http://www.easysoft.com/developer/languages/c/odbc_tutorial.html

Compiler options: -Tx86-coff -MT -Ot -W1 -Gd -Ze -Zx
Linker options: Libraries kernel32.lib user32.lib gdi32.lib comctl32.lib comdlg32.lib advapi32.lib delayimp.lib odbc32.lib; beside this nothing is set.

Thank you for your preparedness to help me. Must be i am doing something really basic wrong.

JohnF

  • Guest
Re: Compiler trouble
« Reply #6 on: July 16, 2008, 12:14:35 PM »
>>Better to ask about what you don't know than to be stupid all your life

Yes.

You had extract_error twice in the code.

You had compiled the app using __cdecl, Windows apps should be compiled using __stdcall

This was not even a completed function, plus the ; on the first line is an error. PLus this function can be located at line 656.

Code: [Select]
static void extract_error(char *fn,SQLHANDLE handle,SQLSMALLINT type);
    SQLHENV env;
    SQLHDBC dbc;
    SQLHSTMT stmt;
    SQLRETURN ret; /* ODBC API return status */
    SQLCHAR outstr[1024];
    SQLSMALLINT outstrlen;

  /* Allocate an environment handle */

SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env*);

See the attached zip file.

John
« Last Edit: July 16, 2008, 12:19:45 PM by JohnF »

Zodra1

  • Guest
Re: Compiler trouble
« Reply #7 on: July 16, 2008, 02:27:39 PM »
Thank you John. Really kind from you that you took the time and solve really BEGINNERS QUESTION. I knew I am doing something utterly basic wrong. I really do not know practically nothing about C language. Until now I was doing application programming in CLIPPER or Visual basic (Access). But there are limitations in Access. I wanted to see how windows programming looks like. Now I will turn to C language basic. If there is something basic and simple like first steps using C or something similar it would help.

I will correct thise errors and hopefully application will compile well.

JohnF

  • Guest
Re: Compiler trouble
« Reply #8 on: July 16, 2008, 03:01:06 PM »
Keep learning.  :)

The zipped project I attached should be ok.

Except I found a slight error generated by the MDI wizard. In this function

Code: [Select]
DWORD GetChildWindowPlacement(HWND hWndClient)
{
WINDOWPLACEMENT wp;
HWND h;
wp.length = sizeof(WINDOWPLACEMENT);
if (h = (HWND)SendMessage(hWndClient, WM_MDIGETACTIVE, 0, 0))
{
GetWindowPlacement(h, &wp);

if (SW_SHOWMAXIMIZED == wp.showCmd)
return WS_MAXIMIZE;
else if (SW_SHOWNORMAL == wp.showCmd)
return 0;
}
return 0;
}

The line

Code: [Select]
if (h = (HWND)SendMessage(hWndClient, WM_MDIGETACTIVE, 0, ))

Should be
Code: [Select]
if ((HWND)0 != (h = (HWND)SendMessage(hWndClient, WM_MDIGETACTIVE, 0, 0)))

This will take away that warning you get

warning #2030: = used in a conditional expression.

John

Zodra1

  • Guest
Re: Compiler trouble
« Reply #9 on: July 16, 2008, 03:57:17 PM »
Yes, the zip project compiles with only one warning about a variable not referenced; but I just noticed that you commented also call to SQLAllocHandle function. Why? This is only first of many calls to SQL API that needs to be processed in order to connect to ODBC data source.

JohnF

  • Guest
Re: Compiler trouble
« Reply #10 on: July 16, 2008, 04:16:21 PM »
I commented out that function extract_error, because you have another one further down the code. You can't have two functions called to same.

Here is the function (with corrections) but you will have to rename it so it does not conflict with the other function of that name.

Code: [Select]
static void extract_error(char *fn, SQLHANDLE handle, SQLSMALLINT type)
{
    SQLHENV env;
    SQLHDBC dbc;
    SQLHSTMT stmt;
    SQLRETURN ret; // ODBC API return status
    SQLCHAR outstr[1024];
    SQLSMALLINT outstrlen;

  // Allocate an environment handle

SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
}

John

Zodra1

  • Guest
Re: Compiler trouble
« Reply #11 on: July 16, 2008, 04:44:05 PM »
Now it's really starting to annoy me. I commented out function at the end of the code and correct the one at the beginnig of code. Below is a compiler result:

Building main.obj.
D:\Pelles_Razvoj\MaFiP'S 3.0\mafips30\main.c(43): error #2001: Syntax error: expected ')' but found 'integer constant'.
D:\Pelles_Razvoj\MaFiP'S 3.0\mafips30\main.c(43): warning #2010: Declaration of 'SQLAllocHandle' does not match previous declaration at C:\Program Files\PellesC\Include\Win\sql.h(433).
D:\Pelles_Razvoj\MaFiP'S 3.0\mafips30\main.c(41): warning #2114: Local 'outstrlen' is not referenced.
D:\Pelles_Razvoj\MaFiP'S 3.0\mafips30\main.c(40): warning #2114: Local 'outstr' is not referenced.
D:\Pelles_Razvoj\MaFiP'S 3.0\mafips30\main.c(39): warning #2114: Local 'ret' is not referenced.
D:\Pelles_Razvoj\MaFiP'S 3.0\mafips30\main.c(38): warning #2114: Local 'stmt' is not referenced.
D:\Pelles_Razvoj\MaFiP'S 3.0\mafips30\main.c(37): warning #2114: Local 'dbc' is not referenced.
D:\Pelles_Razvoj\MaFiP'S 3.0\mafips30\main.c(36): warning #2114: Local 'env' is not referenced.
D:\Pelles_Razvoj\MaFiP'S 3.0\mafips30\main.c(34): warning #2135: Static 'extract_error' is not referenced.
*** Error code: 1 ***
Done.

I will enclose zip of this project. What boders me are the two errors at line 43. It seems I can't call any of necessary SQL API functions at all! I freally do not know why. Why are all API calss to WIndows functions working? Of course i tried also: HANDLE SQLAllochandle ....... but with same result.

JohnF

  • Guest
Re: Compiler trouble
« Reply #12 on: July 16, 2008, 05:42:44 PM »
This line is wrong

SQLRETURN SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env*);

Why don't you use the code I sent?

The function where that is called is void - in other words the function does not return a value, but you trying to return a value.

&env* ------ is wrong. Look at the code I corrected.

John

Zodra1

  • Guest
Re: Compiler trouble
« Reply #13 on: July 17, 2008, 10:00:50 AM »
This part of code that you corrected id commented is it not? (/* to */)
Code: [Select]
/*
static void extract_error(char *fn, SQLHANDLE handle, SQLSMALLINT type)
{
    SQLHENV env;
    SQLHDBC dbc;
    SQLHSTMT stmt;
    SQLRETURN ret; // ODBC API return status
    SQLCHAR outstr[1024];
    SQLSMALLINT outstrlen;

  // Allocate an environment handle

SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
}
*/
If not then I know how to proceed; if it is commented then I don't know how to call all necessary functions which will establish ODBC connect.

Of course I can use your corrected version. But problem remains if all above is true.

JohnF

  • Guest
Re: Compiler trouble
« Reply #14 on: July 17, 2008, 10:24:50 AM »
Do you remember why I commented it out?

John