c++ caling a dll

Hi i have a requirement where a cpp file should load a dll viz gzip.dll.then the cpp should call few methods in the dll.well im able to call the methods with no parameters but im unable to call the methods with parameters.if im trying to call methods with parameters i am getting memory exception in runtime.the program compiles very fine.i actually replicated the code in vb where a similair call has been made to the methods in gzip.dll for compression and decompression.but in cpp it doesnt work for calling these methods with the exact parameters.may be my code has a flaw.


Please c the file.u can find the gzip.dll from C:\WINNT\system32\gzip.dll

Help with Code Tags cplusplus Syntax (Toggle Plain Text)
#include <windows.h>
#include <stdio.h>
int main()
{

HINSTANCE hGetProcIDDLL = LoadLibrary("C:\\WINNT\\system32\\gzip.dll");
printf("dll loaded");
/* get pointer to the function in the dll*/
FARPROC lpfnGetProcessID = GetProcAddress(HMODULE (hGetProcIDDLL),"Decompress");
FARPROC lpfnGetProcessID1 = GetProcAddress(HMODULE (hGetProcIDDLL),"InitDecompression");
FARPROC lpfnGetProcessID2 = GetProcAddress(HMODULE (hGetProcIDDLL),"CreateDecompression");
FARPROC lpfnGetProcessID3 = GetProcAddress(HMODULE (hGetProcIDDLL),"InitCompression");
FARPROC lpfnGetProcessID4 = GetProcAddress(HMODULE (hGetProcIDDLL),"CopyMemory");


// Call CopyMemory(arrDestination(destbeginPos), arrSource(srcbeginPos), length)


// printf("Hello World long decompress!\n");
//printf(1);

typedef long (__stdcall * pICFUNC)(long, BYTE[],long,BYTE[],long,long,long);
typedef long (__stdcall * pICFUNC1)();
typedef long (__stdcall * pICFUNC2)(long,int);
typedef long (__stdcall * pICFUNC3)();
typedef long (__stdcall * pICFUNC4)(BYTE[],BYTE[],long);

typedef UINT (CALLBACK* CopyMemory)(BYTE[],BYTE[],long);
CopyMemory ptrCopyMemory;
ptrCopyMemory = (CopyMemory)GetProcAddress(hGetProcIDDLL,"CopyMemory");
typedef UINT (CALLBACK* InitDecompression)();
InitDecompression ptrInitDecompression;
ptrInitDecompression = (InitDecompression)GetProcAddress(hGetProcIDDLL,"InitDecompression");
//ReturnVal = ptrLockWorkStation();
ptrInitDecompression();
printf("Hello World long decompress!\n");


pICFUNC MyFunction;
MyFunction = pICFUNC(lpfnGetProcessID);
pICFUNC1 MyFunction1;
MyFunction1 = pICFUNC1(lpfnGetProcessID1);
pICFUNC2 MyFunction2;
MyFunction2 = pICFUNC2(lpfnGetProcessID2);
pICFUNC3 MyFunction3;
MyFunction3 = pICFUNC3(lpfnGetProcessID3);
pICFUNC4 MyFunction4;
MyFunction4 = pICFUNC4(lpfnGetProcessID4);


//BYTE[] a=NewByteArray(jb);

unsigned long handle=0;long max=1024;long inu=0;long outu=0;int GZIP_LVL=1;


BYTE *AB={(unsigned char *)"1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"};
// int li= strlen( (char*)AB);
int orglen=100;

//printf("%l", li);


BYTE *oB=new BYTE[43822];
CopyMemory(AB,oB,234);
printf("function defined and parameters ready"+orglen,"%l",orglen);
printf("function defined and parameters ready");
/* The actual call to the function contained in the dll */

handle=MyFunction1();
//handle=MyFunction4(AB,oB,inu);
handle=MyFunction3();
printf("InitDecompression & InitCompression");
MyFunction2(handle,GZIP_LVL);//Call CreateDecompression(handle, GZIP_LVL);
//handle=MyFunction2(handle,GZIP_LVL);
printf("CreateDecompression");

long intMyReturnVal =0;
do
{// int

intMyReturnVal=MyFunction(handle, AB, orglen, oB, max, inu, outu);
orglen = orglen - inu;
}
while(intMyReturnVal=0);

/* Release the Dll */
FreeLibrary(hGetProcIDDLL);
printf("Hello World long decompress!\n");
/* The return val from the dll */
return intMyReturnVal;
}






This is the vb code to access the different dll functions on it.Ill mark the dll function calls in red.


Code For Decompression
=======================

If mvDataFormat = 2 Then
Dim Decompress As String
Dim InfoArr() As String
InfoArr = Split(DataInfo, "~")
If UBound(InfoArr) >= 3 Then
If Val(InfoArr(2)) <> 0 Then
obj_Decompress.OrginalSize = CLng(InfoArr(3))
If Not obj_Decompress.DecompressString(Data) Then
RaiseEvent Error("Decompression Error")
Exit Sub
End If
Data = Left$(Data, CLng(InfoArr(3)))
End If
End If
End If



DecompressionClass
===================

Option Explicit

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)

Private Declare Function InitDecompression Lib "gzip.dll" () As Long
Private Declare Function CreateDecompression Lib "gzip.dll" (ByRef context As Long, ByVal flags As Long) As Long
Private Declare Function Decompress Lib "gzip.dll" (ByVal context As Long, inBytes As Any, ByVal input_size As Long, outBytes As Any, ByVal output_size As Long, ByRef input_used As Long, ByRef output_used As Long) As Long
Private Declare Function DestroyDecompression Lib "gzip.dll" (ByRef context As Long) As Long

Private Declare Function InitCompression Lib "gzip.dll" () As Long
Private Declare Function CreateCompression Lib "gzip.dll" (ByRef context As Long, ByVal flags As Long) As Long
Private Declare Function Compress Lib "gzip.dll" (ByVal context As Long, inBytes As Any, ByVal input_size As Long, outBytes As Any, ByVal output_size As Long, ByRef input_used As Long, ByRef output_used As Long, ByVal compressionLevel As Long) As Long
Private Declare Function DestroyCompression Lib "gzip.dll" (ByRef context As Long) As Long

Private Declare Function ResetDecompression Lib "gzip.dll" (ByVal context As Long) As Long
Private Declare Function ResetCompression Lib "gzip.dll" (ByVal context As Long) As Long

Private Const MAX_BUF As Long = 100000
Private Const GZIP_LVL As Integer = 1

Dim lngCompressedSize As Long
Dim lngDecompressedSize As Long
Dim lngOrginalSize As Long

Public Property Get OrginalSize() As Long
OrginalSize = lngOrginalSize
End Property

Public Property Let OrginalSize(ByVal vSze As Long)
lngOrginalSize = vSze
End Property

Public Property Get CompressedSize() As Long
CompressedSize = lngCompressedSize
End Property

Public Property Let CompressedSize(ByVal vSze As Long)
lngCompressedSize = vSze
End Property

Public Property Get DeCompressedSize() As Long
DeCompressedSize = lngDecompressedSize
End Property

Public Property Let DeCompressedSize(ByVal vSze As Long)
lngDecompressedSize = vSze
End Property

'============================================================================================
' Compression and Decompression of String bytes
'============================================================================================

Public Function CompressByte(ByteArray() As Byte) As Boolean
On Error GoTo Err_Handle:

Dim ResultArr() As Byte
Dim Result() As Byte
Dim OutputBuf(MAX_BUF - 1) As Byte

Dim lngSize As Long
Dim outUsed As Long
Dim inUsed As Long
Dim contextHandle As Long

Dim lRet As Long
Dim tmplength As Long

ReDim Result(0) As Byte
ReDim Preserve ResultArr(UBound(ByteArray))

CompressByte = False

Call CopyMemory(ResultArr(0), ByteArray(0), UBound(ByteArray) + 1)


lngOrginalSize = UBound(ResultArr) + 1
lngDecompressedSize = lngOrginalSize
lngSize = lngOrginalSize

outUsed = 0: inUsed = 0

Call InitCompression Call CreateCompression(contextHandle, GZIP_LVL)

Do

lRet = Compress(ByVal contextHandle, ResultArr(0), lngSize, OutputBuf(0), MAX_BUF, inUsed, outUsed, GZIP_LVL)
If outUsed <> 0 Then
tmplength = UBound(Result)
ReDim Preserve Result(tmplength + outUsed)
Call ArrayCopy(OutputBuf, Result, 0, tmplength, outUsed)
lngSize = lngSize - inUsed
End If

Loop While lRet = 0


If UBound(Result) > 0 Then
Erase ByteArray
ReDim Preserve ByteArray(UBound(Result))
Call CopyMemory(ByteArray(0), Result(0), UBound(Result) + 1)lngCompressedSize = UBound(Result) + 1
CompressByte = True
End If

Call ResetCompression(contextHandle)

Erase Result
Erase ResultArr
Erase OutputBuf

Exit Function
Err_Handle:
Err.Clear

End Function

Public Function DeCompressByte(ByteArray() As Byte) As Boolean
On Error GoTo Err_Handle:

Dim Result() As Byte
Dim buffer(MAX_BUF - 1) As Byte

Dim orglength As Long
Dim contextHandle As Long
Dim outUsed As Long
Dim inUsed As Long
Dim lRet As Long
Dim tmplength As Long

ReDim Result(0) As Byte

orglength = UBound(ByteArray)

outUsed = 0: inUsed = 0
DeCompressByte = False

Call InitDecompression
Call CreateDecompression(contextHandle, GZIP_LVL)
Do
lRet = Decompress(ByVal contextHandle, ByteArray(0), orglength, buffer(0), MAX_BUF, inUsed, outUsed)If outUsed <> 0 Then
tmplength = UBound(Result)
ReDim Preserve Result(tmplength + outUsed)
Call ArrayCopy(buffer, Result, 0, tmplength, outUsed)
orglength = orglength - inUsed
End If

Loop While lRet = 0

Call ResetDecompression(contextHandle)
If UBound(Result) > 0 Then
Erase ByteArray()
ReDim Preserve ByteArray(UBound(Result) - 1)
Call CopyMemory(ByteArray(0), Result(0), UBound(Result))
DeCompressByte = True
End If
Erase Result()
Erase buffer()

Exit Function
Err_Handle:
Err.Clear
End Function

'===================================================================================================================
'///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
'===================================================================================================================

'===================================================================================================================
' Compression and Decompression of Strings
'===================================================================================================================
Public Function CompressString(Text As String) As Boolean
On Error GoTo Err_Handle:
Dim bytes() As Byte
CompressString = False
bytes = String2Byte(Text)
If CompressByte(bytes) Then
Text = Byte2String(bytes)
CompressString = True
End If
Erase bytes()
Exit Function
Err_Handle:
Err.Clear
End Function

Public Function DecompressString(Text As String) As Boolean
On Error GoTo Err_Handle:
Dim bytes() As Byte
DecompressString = False

bytes = String2Byte(Text)
If DeCompressByte(bytes) Then
Text = Byte2String(bytes)
DecompressString = True
End If
Erase bytes()
Exit Function
Err_Handle:
Err.Clear
End Function
'============================================================================================
'////////////////////////////////////////////////////////////////////////////////////////////
'============================================================================================

Public Function Byte2String(dataByt() As Byte) As String
On Error GoTo Err_Handle:
Byte2String = StrConv(dataByt, vbUnicode)
Exit Function
Err_Handle:
Err.Clear
End Function
Public Function String2Byte(dataStr As String) As Byte()
On Error GoTo Err_Handle:
String2Byte = StrConv(dataStr, vbFromUnicode)
Exit Function
Err_Handle:
Err.Clear
End Function

Private Function ArrayCopy(ByRef arrSource() As Byte, ByRef arrDestination() As Byte, Optional ByVal srcbeginPos As Long = 0, Optional ByVal destbeginPos As Long = 0, Optional ByVal length As Long = 0) As Boolean
On Error GoTo Err_Handle:
ArrayCopy = True
If length = 0 Then length = UBound(arrSource) + 1
If srcbeginPos + length > UBound(arrSource) + 1 Then GoTo Err_Handle
If destbeginPos + length > UBound(arrDestination) + 1 Then GoTo Err_Handle

Call CopyMemory(arrDestination(destbeginPos), arrSource(srcbeginPos), length)Exit Function
Err_Handle:
Err.Clear
ArrayCopy = False
End Function
Your VB code loads CopyMemory from kernel32.dll
 
Private Declare Sub CopyMemory Lib "kernel32"


Your C++ code is really messy. You've posted more than is strictly necessary to demonstrate your problem.

Having said that; in your C++ code you attempt to load CopyMemory from gzip.dll. Had you checked the value of CopyMemory, you'd find it was null; that is, CopyMemory was not found in gzip.dll.
closed account (z05DSL3A)
Help with Code Tags cplusplus Syntax (Toggle Plain Text)


[code]
Your code...
[/code]
yes CopyMemory Lib "kernel32" but ttry working on the code ull never be able to call any function from gzip.ddl.is it the problem of the dll kbw.
Depends can show the names of the functions in gzip.dll, but you need to know the signatures of the functions. I've used your signatures (taken from your VB code) in this sample, which doesn't crash, but doesn't compress either. I've just written it to show that you can make a call into gzip.dll without crashing.

BTW, I couldn't find the correct signatures anywhere. The library appears to be an undocumented IIS thing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <windows.h>
#include <cstring>

int main()
{
    typedef LONG (__stdcall *pInitCompression)();
    typedef LONG (__stdcall *pCreateCompression)(LONG *context, LONG flags);
    typedef LONG (__stdcall *pCompress)(
                                LONG context,
                                const BYTE *in,
                                LONG insize,
                                BYTE *out,
                                LONG outsize,
                                LONG *inused,
                                LONG *outused,
                                LONG compression_level);
    typedef LONG (__stdcall *pDestroyCompression)(LONG context);
    typedef LONG (__stdcall *pDeInitCompression)();

    if (HINSTANCE hInst = LoadLibrary("C:\\WINDOWS\\ServicePackFiles\\i386\\gzip.dll"))
    {
        pInitCompression	InitCompression	= (pInitCompression)GetProcAddress(hInst, "InitCompression");
        pCreateCompression	CreateCompression	= (pCreateCompression)GetProcAddress(hInst, "CreateCompression");
        pCompress		Compress		= (pCompress)GetProcAddress(hInst, "Compress");
        pDestroyCompression	DestroyCompression	= (pDestroyCompression)GetProcAddress(hInst, "DestroyCompression");
        pDeInitCompression	DeInitCompression	= (pDeInitCompression)GetProcAddress(hInst, "DeInitCompression");

        if (InitCompression && CreateCompression && Compress && DestroyCompression && DeInitCompression)
        {
            const LONG GZIP_LVL = 1;
            LONG ctx = 0;

            char in[] = "Can we compress this?";
            LONG insize = (LONG)strlen(in);

            unsigned char out[100];
            LONG outsize = (LONG)sizeof(out);
            memset(out, 0, sizeof(out));

            LONG inused = 0;
            LONG outused = 0;

            InitCompression();
            CreateCompression(&ctx, GZIP_LVL);
            if (ctx)
            {
                Compress(ctx, (BYTE*)in, insize, out, outsize, &inused, &outused, 9L);
                DestroyCompression(ctx);
            }

            DeInitCompression();
        }

        FreeLibrary(hInst);
    }

    return 0;
}
Last edited on
HI i have made great progress with the help of the code u provided me

U can c the following changes i made to ur code above according to the Decompressing logic used in the VISUAL BASIC CLASS FILE I PROVIDED.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107

//CODE FOR DECOMPRESSION 
#include <windows.h>
#include <stdio.h>
#include <cstring>
void ArrayCopy(BYTE *arrSource,BYTE *arrDestination,LONG srcbeginPos,LONG destbeginpos,LONG length) 
{
//int c=a+b;
//return c;
	typedef LONG (__stdcall *pCopyMemory)(BYTE *in,BYTE *out);
	if(length == 0){ length = (LONG)sizeof(arrSource)+ 1;}
	 if (HINSTANCE hInst = LoadLibrary("C:\\WINNT\\system32\\KERNEL32.dll"))
	 {
		pCopyMemory	CopyMemory	= (pCopyMemory)GetProcAddress(hInst, "CopyMemory");
		//CopyMemory(arrDestination(destbeginPos), arrSource(srcbeginPos), length);
		if (CopyMemory)
        {
			CopyMemory(arrDestination,arrSource,length);
		}
	 }
//return 0;
} 
int main()
{

typedef LONG (__stdcall *pInitDecompression)();
    typedef LONG (__stdcall *pCreateDecompression)(LONG *context, LONG flags);
    typedef LONG (__stdcall *pDecompress)(
                                LONG context,
                                const BYTE *in,
                                LONG insize,
                                BYTE *out,
                                LONG outsize,
                                LONG *inused,
                                LONG *outused);
    typedef LONG (__stdcall *pDestroyDecompression)(LONG context);
    typedef LONG (__stdcall *pDeInitDecompression)();

  //  if (HINSTANCE hInst = LoadLibrary("C:\\WINDOWS\\ServicePackFiles\\i386\\gzip.dll"))
	  if (HINSTANCE hInst = LoadLibrary("C:\\WINNT\\system32\\gzip.dll"))
    {
        pInitDecompression	InitDecompression	= (pInitDecompression)GetProcAddress(hInst, "InitDecompression");
        pCreateDecompression	CreateDecompression	= (pCreateDecompression)GetProcAddress(hInst, "CreateDecompression");
        pDecompress		Decompress		= (pDecompress)GetProcAddress(hInst, "Decompress");
        pDestroyDecompression	DestroyDecompression	= (pDestroyDecompression)GetProcAddress(hInst, "DestroyDecompression");
        pDeInitDecompression	DeInitDecompression	= (pDeInitDecompression)GetProcAddress(hInst, "DeInitDecompression");

        if (InitDecompression && CreateDecompression && Decompress && DestroyDecompression && DeInitDecompression)
        {
            const LONG GZIP_LVL = 1;
            LONG ctx = 0;

            char in[] = "gvnvcncv";
            LONG insize = (LONG)strlen(in);

            //unsigned char out[100];
			BYTE *out=new BYTE[100];
		
            LONG outsize = (LONG)sizeof(out);
            memset(out, 0, sizeof(out));
			
		
			BYTE *Result=new BYTE[insize];
			LONG tmplength=0;


 
            LONG inused = 0;
            LONG outused = 0;LONG lret=0;
		     InitDecompression();
            CreateDecompression(&ctx, GZIP_LVL);
            if (ctx)
            {
				do{
					lret=Decompress(ctx, (BYTE*)in, insize, out, outsize, &inused, &outused);
					if(outused>0){
									tmplength =(LONG)sizeof(Result);
									Result=new BYTE[tmplength+outused];
									ArrayCopy(out, Result, 0, tmplength, outused);										
									insize = insize - inused;
								}
					 printf("Hello Worldss!\n");
					}while(lret=0);
			//	Decompress(ctx, (BYTE*)AB, insize, out, outsize, &inused, &outused);
				
                DestroyDecompression(ctx);
				//messagebox("bcnc","sdbsdjk", MB_OK);

			 printf("Hello World!\n");
		/*	 if((LONG)sizeof(Result)>0){
			 
			 
			 }*/
            }
 
            DeInitDecompression();
        }

        FreeLibrary(hInst);
    }

    return 0;
}



Last edited on
But the problem is i have to decompress the compressed data returned by our local url using gzip.dll.How can it be done usuaing the above program in hand.
The url is as follows

http://81.19.49.223:8201/commonwebprice/pricefetcher?symbols=JPY&period=T&format=2

The data between <START~JPY^T~4230~25976> and <END> tag returned by the source of the above URL Response

The url returns data compressed using gzip.dll.The program has to decompress using gzip.dl decompress algorithm.In the above cpp code provided how can it be applicable.
Topic archived. No new replies allowed.