让a / d转换器示例程序在C ++ Windows程序中工作

时间:2017-06-26 10:33:45

标签: c++ winapi visual-studio-2017 analog-digital-converter

我从我的模拟到数字转换器的开发人员那里获得了一个C windows程序的示例代码,并希望它在Visual Studio 2016中作为Windows c ++项目运行。

作为C ++的初学者,Windows API和转换器的这个通用库我现在已经不知所措了。计划是通过使代码工作来学习,但是现在很难找到问题的位置,而且没有时间从头开始学习所有东西(尽管我会尝试一下)

这是我的设置:Windows 7 64位,MS Visual Studio社区2017,USB 1608fs加上带通用库的A / D转换器

这些是我目前得到的错误:

  
      
  • “HANDLE”类型的值不能用于初始化“HINSTANCE”类型的实体(第83行)
  •   
  • “HGDIOBJ”类型的值不能用于初始化“HBRUSH”类型的实体(第86行)
  •   
  • “HANDLE”类型的参数与参数类型“HINSTANCE”不兼容(第93行)
  •   

我到目前为止采取的行动:

  • 在MS Visual Studio中创建一个新的空win32项目
  • 复制示例代码
  • 将头文件和库从转换器复制到同一文件夹
  • 将字符集从unicode切换为多字节
  • 关闭预编译的标题
  • 包含预编译标题中缺少的标题

以下是代码:

#include <windows.h>                    /* Compiler's include files's */
#include <string.h>
#include <stdio.h>
#include "cbw.h"                        /* Universal Library's include file */
#include "targetver.h"
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
#define BOARD_NUM      0                /* Number of A/D board as defined with InstaCal */
#define ADRANGE        BIP5VOLTS        /* A/D voltage range */
#define TIMER_NUM      1                /* Windows timer used by this program */

HWND hWndMain;                          /* handle for main window */

LONG FAR PASCAL MainMessageHandler(HWND, UINT, WPARAM, LPARAM);

/************************************************************************
*
* Name:      WinMain
*
* Arguments: hInstance - the instance handle for the program
*            hPrevInstance - the class name of the application (not used)
*            CmndLine - command line was called with (not used)
*            CmndShow - indicates how to display window
*
* This is the entry point to the program. It gets called by Windows
* to start the program up.  This routine creates the main window,
* initializes a timer, and then falls into the main Windows Get
* message/Dispatch message loop.
*
************************************************************************/

int PASCAL
WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR CmdLine, int nCmdShow)
{
    MSG       msg;                      /* MSG structure to pass to windows proc */
    WNDCLASS  wndclass;
    char      *AppName;                 /* Name for the window */

    cbErrHandling(PRINTALL, STOPALL);  /* Set library's error handling */

    CmdLine = NULL;                     /* Not used */
    AppName = "WINCDEMO";               /* The name of this application */
    if (!hPrevInstance)
    {
        wndclass.style = CS_HREDRAW | CS_VREDRAW;
        wndclass.lpfnWndProc = MainMessageHandler;
        wndclass.cbClsExtra = 0;
        wndclass.cbWndExtra = 0;
        wndclass.hInstance = hInstance;
        wndclass.hIcon = LoadIcon(hInstance, AppName);
        wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
        wndclass.hbrBackground = GetStockObject(WHITE_BRUSH);
        wndclass.lpszMenuName = AppName;
        wndclass.lpszClassName = AppName;
        RegisterClass(&wndclass);
    }

    /* create application's Main window                                    */
    hWndMain = CreateWindow(AppName,                  /* Window class name          */
        "AIn Demo",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,           /* Use default X, Y            */
        CW_USEDEFAULT,           /* Use default X, Y            */
        GetSystemMetrics(SM_CXSIZE) * 12,   /* x - fit text     */
        GetSystemMetrics(SM_CYSIZE) * 10,  /* y - fit text      */
        NULL,                    /* Parent window's handle      */
        NULL,                    /* Default to Class Menu       */
        hInstance,               /* Instance of window          */
        NULL);                   /* Create struct for WM_CREATE */


    if (hWndMain == NULL)
    {
        MessageBox(NULL, "Could not create window in WinMain", NULL, MB_ICONEXCLAMATION);
        return (1);
    }

    ShowWindow(hWndMain, nCmdShow);     /* Display main window      */
    UpdateWindow(hWndMain);

    //    /* Start a 500ms timer to update display */
    if (!SetTimer(hWndMain, TIMER_NUM, 50, NULL))
    {
        MessageBox(NULL, "Error starting Windows timer", NULL, MB_OK |
            MB_ICONEXCLAMATION);
        return (1);
    }

    while (GetMessage(&msg, NULL, 0, 0)) /* Main message loop */
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    UnregisterClass(AppName, hInstance);
    return (msg.wParam);
}



/************************************************************************
*
* Name:      MainMessageHandler
*
* Arguments: hWnd - handle to the window
*            Message - message that needs to be handled
*            hParam - message parameter
*            lParam - message parameter
*
* This is the message dispatcher that gets called whenever Windows
* sends a message to this window.  WinMain started up a timer that
* sends a message every 1/2 sec.  When the message (WM_TIMER)is received
* by this routine, it reads the A/D.
* It also causes a screen update which will automatically generate a
* WM_PAINT message.  The WM_PAINT handler takes care of converting the
* raw A/D values to voltages and printing them in the Window.
*
************************************************************************/

LONG FAR PASCAL
MainMessageHandler(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
    HDC        hDC;                     /* handle for the display device  */
    PAINTSTRUCT ps;                     /* holds PAINT information        */
    TEXTMETRIC tm;                      /* holds TEXT information         */

    static HRGN hRgn;                   /* Rectangle region Handles       */
    static int  CharWidth, CharHeight;
    static unsigned short DataVal;
    int        x, y;
    char       OutString[80], *str;
    float      Voltage;

    switch (Message)                    /* Windows Message Loop           */
    {
    case WM_CREATE:
        hDC = GetDC(hWndMain);      /* Get the device context for window */
        GetTextMetrics(hDC, &tm);   /* From context, get size of font */
        CharWidth = tm.tmAveCharWidth;
        CharHeight = tm.tmHeight + tm.tmExternalLeading;
        ReleaseDC(hWndMain, hDC);
        hRgn = CreateRectRgn(0, 0, CharWidth * 30, CharHeight * 12);
        break;

    case WM_TIMER:                  /* All Timer Events Processed Here */
        InvalidateRgn(hWndMain, hRgn, FALSE);  /* Force screen update  */
        break;

    case WM_PAINT:                  /* Repaint client area of window */
        hDC = BeginPaint(hWndMain, &ps);
        x = CharWidth * 2;          /* Position cursor within window */
        y = CharHeight;             /* One line down and 2 chars in */
        str = "         A/D Info";  /* Print title */
        TextOut(hDC, x, y, str, strlen(str));

        y += CharHeight;            /* Print current index */
        cbAIn(BOARD_NUM, 0, ADRANGE, &DataVal);

        y += CharHeight * 2;            /* Print raw data value */
        sprintf(OutString, "Raw A/D value = %u    ", DataVal);
        TextOut(hDC, x, y, OutString, strlen(OutString));

        y += CharHeight;    /* Convert raw A/D to volts and print */
        cbToEngUnits(BOARD_NUM, ADRANGE, DataVal, &Voltage);
        sprintf(OutString, "Voltage = %.2f    ", Voltage);
        TextOut(hDC, x, y, OutString, strlen(OutString));

        SetTextAlign(hDC, TA_LEFT | TA_TOP);
        EndPaint(hWndMain, &ps);
        break;

    case WM_CLOSE:                      /* Close the window */
        DestroyWindow(hWnd);
        if (hWnd == hWndMain)
            PostQuitMessage(0);         /* Send message to Quit application */
        break;

    default:
        return (DefWindowProc(hWnd, Message, wParam, lParam));
    }
    return (0l);
}

1 个答案:

答案 0 :(得分:1)

您应该首先了解您所面临的错误,而不是随意漫游在copypasting员工周围并开启和关闭事物。例如,第一个错误:您应该在MSDN上查找HANDLEHINSTANCE是什么,并找出从哪里获取它们。您从hInstance获得WinMain。您应该在MSDN for WinMain上查找,并确定您使用的签名有些错误,您应该使用

int CALLBACK WinMain(
  _In_ HINSTANCE hInstance,
  _In_ HINSTANCE hPrevInstance,
  _In_ LPSTR     lpCmdLine,
  _In_ int       nCmdShow
)

因此wndclass.hInstance = hInstance; HANDLEHINSTANCE的{​​{1}}分配错误应该消失(假设我已经指出了此错误的正确行,因为您只指定了行)。

等等。