C ++ dll函数中的编译错误 [英] compile error in c++ dll function

查看:74
本文介绍了C ++ dll函数中的编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用C ++编写函数,需要将其编译为dll文件
我想从其他应用程序使用此dll
其他应用程序会将值作为端口号,波特率等推送到dll文件中...
以下是代码,请帮助进行编译.从comport阅读

Hi, i tried to write function in c++ and need to compile it as dll file
i want to use this dll from my other application
the other application will push values to dll file as portnumber, baudrate, etc...
below is the code, kindly help to compile & to read from comport

#include "stdafx.h"
#include "rs232.h"


int GetWeight( short, wchar_t * Data[], wchar_t **Output, SIZE_T *OutputSize )
{
    int result = 1;

    if( Data[0] )
    {
        *OutputSize = 50;
        *Output = (wchar_t *) CoTaskMemAlloc(*OutputSize);

        if( *Output )
        {
            wchar_t *EndPtr;
            // the below values are pushed from my other application
            double v11 = wcstod(Data[0], &EndPtr);  // Port Name 
            double v12 = wcstod(Data[1], &EndPtr);  // Baud Rate
            double v13 = wcstod(Data[2], &EndPtr);  // Data Bits
            double v14 = wcstod(Data[3], &EndPtr);  // Stop Bits
            double v15 = wcstod(Data[4], &EndPtr);  // Parity

            int portNum = int (v11);
            int baudRate = int (v12);
            int dataBit = int (v13);
            int stopBit = int (v14);
            int parity = int (v15);

           unsigned char* v16;
         

            openPort(portNum,baudRate,dataBit,parity,stopBit);
            v16 = readPort(portNum);

            // need to get comport reading as v16

            swprintf(*Output, L"%.10lf", v16);
            result = 0;
        }
    }

    return result;
}

////

void openPort(int port_num , int baudrate ,int databits, int parity, int stopbit) {

    if(OpenComport(port_num, baudrate,databits,parity,stopbit)) {
        printf("Can not open comport");
        return 1;
    }
    return 0;
}



unsigned char* readPort(int cport_nr)
{

    unsigned char buf[4096];
    unsigned char buf2[4096];

    int n,i;
    n = PollComport(cport_nr, buf, 4095);

    if(n > 0) {
      buf[n] = 0;   //always put a "null" at the end of a string!

      for(i=0; i < n; i++) {
        if(buf[i] < 32)  // replace unreadable control-codes by dots
        {
          buf[i] = '.';
        }
      }
    } else {
        return buf2;
    }
    return buf;
}

推荐答案

如果不编译代码来查看错误,则可以在源代码中看到一些错误和其他值得注意的地方:

  • 您正在将串行参数作为字符串传递,将其转换为double值然后转换为int值.为什么不传递整数呢?
  • 串行端口是打开的,但从未关闭.因此,只有第一个GetWeight()调用会成功.但是,由于缺少对openPort()的返回值的检查,您将错过该失败.
  • 您的readPort()函数使用局部变量返回数据.这将不起作用.
  • 您的readPort()函数从端口读取一个字符串.但是,您正在将返回的指针传递给swprintf(),其格式字符串应为double.
Without compiling the code to see the errors, some errors and other noteable points can be seen in the source:

  • You are passing the serial parameters as strings, converting them to double and then to int. Why just not pass integers?
  • The serial port is opened but never closed. So only the first call to GetWeight() will succeed. But you will miss the failure due to the missing check of the return value of openPort().
  • Your readPort() functions returns data using local variables. This will not work.
  • Your readPort() function reads a string from the port. But you are passing the returned pointer to swprintf() with a format string expecting a double.


这篇关于C ++ dll函数中的编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆