在C Windows程序中获取A/D转换器示例程序

Getting a/d converter example program to work in a C++ windows program

本文关键字:程序 转换器 例程 获取 Windows      更新时间:2023-10-16

我从我的模拟转换器的开发人员到数字转换器的开发人员中获得了以下示例代码,并希望将其运行为Visual Studio 2016中的Windows C 项目。

作为C 的初学者,Windows API和这个通用库的转换器,我现在对故障排除不知所措。计划是通过使代码工作来学习,但是现在很难找出问题在哪里,并且没有时间从头开始学习所有内容(尽管我在旁边尝试了(

这是我的设置:Windows 7 64bit,MS Visual Studio Community 2017,USB 1608FS加上A/D转换器,其通用库

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

  • 类型"句柄"的值不能用于初始化类型" hinstance"的实体(第83行(
  • 类型" hgdiobj"的值不能用于初始化类型" hbrush"的实体(第86行(
  • 类型"句柄"的参数与参数类型" 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);
}

,而不是在复复播人员周围随机漫游并打开东西,而是首先应该了解所面临的错误。例如,第一个错误:您应该查找MSDN,HANDLEHINSTANCE是什么,并弄清楚您从哪里得到它们。您从WinMain获得hInstance。您应该在MSDN查找WinMain,并确定您使用的签名是错误的,您应该使用

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

因此,将wndclass.hInstance = hInstance;分配给HANDLEHINSTANCE的错误应该消失(假设我已经指出了此错误的正确行,因为您仅指定了行(。

等等。