Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DLLLoad returns 0 (Zero)

Status
Not open for further replies.

rafcrumen

Programmer
Dec 20, 2014
1
MX
Hello,

I am new in POS world and c++ language.
I need to create a DLL function that receives a text reference parameter.
DLLLoad Always returns 0 (Zero)

Please what am I doing wrong.

thank you very much

ISL File:

var hODBCDLL: N9
EVENT INQ: 1
Var fecha: A200
DLLLoad hODBCDLL, "Holamundo.dll"
InfoMessage "ID ", hODBCDLL

DLLCall_CDECL hODBCDLL, obtenerfecha(ref fecha)
InfoMessage "fecha", fecha
ENDEVENT

this is the h file

#include <time.h>

#ifndef HOLAMUNDO_H
#define HOLAMUNDO_H

class Holamundo {
public:
Holamundo();
Holamundo(const Holamundo& orig);
virtual ~Holamundo();
__declspec(dllexport) void obtenerfecha(char* fecha);
private:

};

#endif /* HOLAMUNDO_H */



this is the cpp file:


#include "Holamundo.h"

Holamundo::Holamundo() {
}

Holamundo::Holamundo(const Holamundo& orig) {
}

Holamundo::~Holamundo() {
}

void Holamundo::eek:btenerfecha(char* fecha)
{
time_t now = time(0);
struct tm tstruct;
tstruct = *localtime(&now);
strftime(fecha, sizeof(fecha), "%Y-%m-%d.%X", &tstruct);
}
 
You need to setup the DLL so that it can be imported. I can't remember why this didn't work - I think it was because the COM port was locked and not because the code was wrong, but in all honesty, I don't remember and I'm not going to debug this on Christmas Eve ;)

Header
Code:
[COLOR=#0000FF]#ifndef[/color] __MAIN_H__
[COLOR=#0000FF]#define[/color] __MAIN_H__
 
[COLOR=#0000FF]#include[/color] [COLOR=#A31515]&lt;windows.h&gt;[/color]
 
[COLOR=#0000FF]#ifdef[/color] BUILD_DLL
    [COLOR=#0000FF]#define[/color] DLL_EXPORT [COLOR=#0000FF]__declspec[/color]([COLOR=#0000FF]dllexport[/color])
[COLOR=#0000FF]#else[/color]
    [COLOR=#0000FF]#define[/color] DLL_EXPORT [COLOR=#0000FF]__declspec[/color]([COLOR=#0000FF]dllimport[/color])
[COLOR=#0000FF]#endif[/color]
 
 
[COLOR=#0000FF]#ifdef[/color] __cplusplus
[COLOR=#0000FF]extern[/color] [COLOR=#A31515]&quot;C&quot;[/color]
{
[COLOR=#0000FF]#endif[/color]
 
[COLOR=#0000FF]void[/color] DLL_EXPORT SetComPort([COLOR=#0000FF]int[/color] comPort, [COLOR=#0000FF]int[/color] buadRate, [COLOR=#0000FF]int[/color] parityType, [COLOR=#0000FF]int[/color] dataBits, [COLOR=#0000FF]int[/color] stopBits);
[COLOR=#0000FF]void[/color] DLL_EXPORT PrintQRCode([COLOR=#0000FF]char[/color]* data, [COLOR=#0000FF]int[/color] fandc);
 
[COLOR=#0000FF]#ifdef[/color] __cplusplus
}
 
[COLOR=#0000FF]#endif[/color]
 
[COLOR=#0000FF]#endif[/color] [COLOR=#008000]// __MAIN_H__[/color]


Code file:


Code:
[COLOR=#0000FF]#include[/color] [COLOR=#A31515]&quot;main.h&quot;[/color]
[COLOR=#0000FF]#include[/color] [COLOR=#A31515]&quot;stdio.h&quot;[/color]
 
[COLOR=#0000FF]static[/color] [COLOR=#0000FF]int[/color] cPort = 1;
[COLOR=#0000FF]static[/color] [COLOR=#0000FF]int[/color] bRate = 9600;
[COLOR=#0000FF]static[/color] [COLOR=#0000FF]int[/color] pType = 0;
[COLOR=#0000FF]static[/color] [COLOR=#0000FF]int[/color] dBits = 8;
[COLOR=#0000FF]static[/color] [COLOR=#0000FF]int[/color] sBits = 1;
 
[COLOR=#0000FF]void[/color] DLL_EXPORT SetComPort([COLOR=#0000FF]int[/color] comPort, [COLOR=#0000FF]int[/color] baudRate, [COLOR=#0000FF]int[/color] parityType, [COLOR=#0000FF]int[/color] dataBits, [COLOR=#0000FF]int[/color] stopBits)
{
    cPort = comPort;
    bRate = baudRate;
    pType = parityType;
    dBits = dataBits;
    sBits = stopBits;
}
 
[COLOR=#0000FF]void[/color] DLL_EXPORT PrintQRCode([COLOR=#0000FF]char[/color]* data, [COLOR=#0000FF]int[/color] fandc)
{
    [COLOR=#0000FF]char[/color] comFile&#91;32&#93; = [COLOR=#A31515]&quot;\\\\.\\COM&quot;[/color];
    [COLOR=#0000FF]char[/color] scratch&#91;10&#93;;
    sprintf(scratch, [COLOR=#A31515]&quot;%d&quot;[/color], cPort);
    strcat(comFile, scratch);
 
    HANDLE hComm;
hComm = CreateFile(
comFile,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
0
);
 
DCB dcb = {0};
[COLOR=#0000FF]if[/color] (GetCommState(hComm, &amp;dcb))
    {
        dcb.BaudRate = bRate;
        dcb.ByteSize = dBits;
        [COLOR=#0000FF]switch[/color] (pType)
        {
            [COLOR=#0000FF]case[/color] 2:
                dcb.Parity = EVENPARITY;
                [COLOR=#0000FF]break[/color];
            [COLOR=#0000FF]case[/color] 1:
                dcb.Parity = ODDPARITY;
                [COLOR=#0000FF]break[/color];
            [COLOR=#0000FF]case[/color] 0:
                dcb.Parity = NOPARITY;
                [COLOR=#0000FF]break[/color];
            [COLOR=#0000FF]default[/color]:
                dcb.Parity = NOPARITY;
        }
        dcb.StopBits = sBits - 1;
    }
 
    OVERLAPPED osWrite = {0};
    DWORD dwWritten;
DWORD dwRes;
BOOL fRes;
 
byte storeQRCode&#91;&#93; = {
0x1D, 0x28, 0x6B, (byte)(strlen(data) + 3), 0x00, 0x31, 0x50, 0x30
};
 
byte modSizeCenterPrintCode&#91;&#93; = {
0x1D, 0x28, 0x6B, 0x04, 0x00, 0x31, 0x41, 0x32, 0x00,
0x1D, 0x28, 0x6B, 0x03, 0x00, 0x31, 0x43, 0x07,
0x1b, 0x61, 0x01,
0x1D, 0x28, 0x6B, 0x03, 0x00, 0x31, 0x51, 0x30
};
 
byte feedAndCut&#91;&#93; = { 27, 64, 0xA, 0x1d, 0x56, 66, 30 };
 
byte dataToPrint&#91; 8 + strlen(data) + 28 + 7 &#93;;
memcpy(dataToPrint, storeQRCode, 8);
memcpy(dataToPrint + 8, data, strlen(data));
memcpy(dataToPrint + 8 + strlen(data), modSizeCenterPrintCode, 28);
[COLOR=#0000FF]if[/color] (fandc == 1)
        memcpy(dataToPrint + 8 + strlen(data) + 28, feedAndCut, 7);
 
    osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
 
    [COLOR=#0000FF]if[/color] (!WriteFile(hComm, dataToPrint, 8 + strlen(data) + 28, &amp;dwWritten, &amp;osWrite))
    {
        [COLOR=#0000FF]if[/color] (GetLastError() != ERROR_IO_PENDING)
        {
            fRes = FALSE;
        }
        [COLOR=#0000FF]else[/color]
        {
            dwRes = WaitForSingleObject(osWrite.hEvent, INFINITE);
            [COLOR=#0000FF]switch[/color] (dwRes)
            {
            [COLOR=#0000FF]case[/color] WAIT_OBJECT_0:
                [COLOR=#0000FF]if[/color] (!GetOverlappedResult(hComm, &amp;osWrite, &amp;dwWritten, FALSE))
                    fRes = FALSE;
                [COLOR=#0000FF]else[/color]
                    fRes = TRUE;
                [COLOR=#0000FF]break[/color];
            [COLOR=#0000FF]default[/color]:
                fRes = FALSE;
                [COLOR=#0000FF]break[/color];
            }
        }
    }
    [COLOR=#0000FF]else[/color]
        fRes = TRUE;
}
 
[COLOR=#0000FF]extern[/color] [COLOR=#A31515]&quot;C&quot;[/color] DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
    [COLOR=#0000FF]switch[/color] (fdwReason)
    {
        [COLOR=#0000FF]case[/color] DLL_PROCESS_ATTACH:
            [COLOR=#008000]// attach to process[/color]
            [COLOR=#008000]// return FALSE to fail DLL load[/color]
            [COLOR=#0000FF]break[/color];
 
        [COLOR=#0000FF]case[/color] DLL_PROCESS_DETACH:
            [COLOR=#008000]// detach from process[/color]
            [COLOR=#0000FF]break[/color];
 
        [COLOR=#0000FF]case[/color] DLL_THREAD_ATTACH:
            [COLOR=#008000]// attach to thread[/color]
            [COLOR=#0000FF]break[/color];
 
        [COLOR=#0000FF]case[/color] DLL_THREAD_DETACH:
            [COLOR=#008000]// detach from thread[/color]
            [COLOR=#0000FF]break[/color];
    }
    [COLOR=#0000FF]return[/color] TRUE; [COLOR=#008000]// succesful[/color]
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top