[BCB 6.0]
我一直用VC,不过最近一个项目,一个哥们给了我一个动态库,导出函数的参数竟然给我 AnsiString &, 好么,不用 BCB 都不行。好在我的程序也不大,而且没用MFC,就直接把代码拷过来用,结果就出错了。一调试,在我调用一个函数(此函数的参数是 char * buf)的时候, buf 指向的内容竟然会自己发生改变,就是说在我调用之前,buf 开始的100 个字节里面都是0,但是调用函数之后 buf 指向的内容变了。不解
这个buf是这样声明的: char buf[100];
后来我用new : char * buf = new char[100];
上述问题倒是没了,好,管他呢,我就把Compile选项改成了release。结果...
运行出错。彻底晕了,各位,我第一次用BCB,是不是我用的不对?

解决方案 »

  1.   

    把你的代码贴出来,我看看,我一前就是用BCB的
      

  2.   

    #include <vcl.h>
    #include <windows.h>
    #pragma hdrstop
    #pragma argsused
    BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fwdreason, LPVOID lpvReserved)
    {
            return 1;
    }
    #include "des3.h"
    #define MAXLEN 250/////////////////////////////////////////////////////////////////////
    //
    // function: ReadSection
    // purpose: 从pIn中读取一段数据到pOut (从pIn开始直到遇到一个'\t'或"\r\n"为一段)
    // parameters:
    // pIn - [in] 要读取的数据的起始位置
    // pOut - [out] 返回一段数据
    // return: 返回读取的长度
    //
    int ReadSection(char * pIn, char * pOut)
    {
    int iRet = 0;
    if (!pIn || !pOut)
    return iRet;    int i;
    for (i = 0; pIn[i]; i++, iRet++)
    {
            if ('\0' == pIn[i])
                break;
    if ('\t' == pIn[i])
    {
    iRet++;
    break;
    }
    if ('\r' == pIn[i])
    {
    iRet += 2;
    break;
    }
    pOut[i] = pIn[i];
    }
    pOut[i] = '\0'; return iRet;
    }extern "C" __declspec(dllexport) signed char * __stdcall decode(signed char * pStr,int iLen,int &iCodeLen)
    {
    if (NULL == pStr)
    return NULL;
    if (iLen < 50)
    return NULL; HMODULE hModule = LoadLibrary("des3.dll");
    if (!hModule)
    return NULL; void (* desDecrypt)(AnsiString &, AnsiString &) = NULL;
    desDecrypt = (void (__cdecl*)(AnsiString &, AnsiString &))GetProcAddress(hModule,"desDecrypt");
    if (!desDecrypt)
    {
    FreeLibrary(hModule);
    return NULL;
    }

        char buf[MAXLEN];
        char buf2[MAXLEN];
        char buf3[10];
        AnsiString str1, str2; char * pTmp;
    pTmp = new char[iLen];
    if (!pTmp)
    return NULL;
    memset(pTmp, 0, iLen);
    int iPos = 0; // Current position
        int iRead = 0; ////////////////////////////////////
    //Read Header - begin    iRead = ReadSection((char *)pStr + iPos, buf);
        if (!iRead) return NULL;
    iPos += iRead;
    strcat(pTmp, buf);
    strcat(pTmp, " ");    iRead = ReadSection((char *)pStr + iPos, buf);
        if (!iRead) return NULL;
    iPos += iRead;
        strcat(pTmp, buf);
    strcat(pTmp, " ");    iRead = ReadSection((char *)pStr + iPos, buf2);
        if (!iRead) return NULL;
    iPos += iRead;
    buf2[strlen(buf2) - 1] = '\0';
    buf2[strlen(buf2) - 1] = '\0';    iRead = ReadSection((char *)pStr + iPos, buf);
        if (!iRead) return NULL;
    iPos += iRead;
        memcpy(buf3, buf + 4, 5);
        buf[4] = '-';
        memcpy(buf + 5, buf3, 2);
        buf[7] = '-';
        memcpy(buf + 8, buf3 + 2, 3);
    strcat(pTmp, buf);
    strcat(pTmp, " "); strcat(pTmp, buf2);
    strcat(pTmp, "\r\n");    iRead = ReadSection((char *)pStr + iPos, buf);
        if (!iRead) return NULL;
    iPos += iRead; //Read Header - end
    //////////////////////////////////// ////////////////////////////////////
    //Decode - begin    bool bNewLine = false;
        do
        {
            memset(buf, 0, MAXLEN);
        iPos += ReadSection((char *)pStr + iPos, buf);
            if (bNewLine)
            {
                str1 = buf;
                desDecrypt(str1, str2);
                strcat(pTmp, str2.c_str());
             strcat(pTmp, "\r\n");
            }
            else
            {
                strcat(pTmp, buf);
                strcat(pTmp, " ");
            }
            bNewLine = !bNewLine;
        }while (iLen > iPos); //Decode - end
    /////////////////////////////////////    strcpy(pStr, pTmp); if (pTmp)
    delete pTmp; if (hModule)
    FreeLibrary(hModule); return pStr;
    }
      

  3.   

    上面的 decode 是我的导出函数,就在下面出的错:
    ---------------------------------------------------
        bool bNewLine = false;
        do
        {
            memset(buf, 0, MAXLEN);
        iPos += ReadSection((char *)pStr + iPos, buf); //<<<---这里!
            if (bNewLine)
            {
                str1 = buf;
                desDecrypt(str1, str2);
                strcat(pTmp, str2.c_str());
             strcat(pTmp, "\r\n");
            }
            else
            {
                strcat(pTmp, buf);
                strcat(pTmp, " ");
            }
            bNewLine = !bNewLine;
        }while (iLen > iPos);
      

  4.   

    int ReadSection(char * pIn, char * pOut)
    {
    int iRet = 0;
    if (!pIn || !pOut)
    return iRet;    int i;
    for (i = 0; pIn[i]; i++, iRet++)
    {
            if ('\0' == pIn[i])//这里不对吧!!
            {
                如果i=0时,iRe=0,就有问题了!!
                break;
            }
    if ('\t' == pIn[i])
    {
    iRet++;
    break;
    }
    if ('\r' == pIn[i])
    {
    iRet += 2;
    break;
    }
    pOut[i] = pIn[i];
    }
    pOut[i] = '\0'; return iRet;
    }
      

  5.   

    你的计算数据长度两个不统一,可能就越界了!
    if ('\t' == pIn[i])
    {
    iRet++;
    break;
    }
    if ('\r' == pIn[i])
    {
    iRet += 2;
    break;
    }
      

  6.   

    To  yening0914(大山) :
    你说的问题不会出现。而且我的代码放到VC中就没有问题了。
    怀疑就是BCB的问题