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

#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

This article has been dead for over six months. Start a new discussion instead.