Thread: Using Zlib1.dll

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    4

    Using Zlib1.dll

    Hi,

    As a bit of fun and a "nice to have" I'm trying to use the zlib1.dll to compress and decompress strings on the fly. I'm sure it'll come in useful someday.
    Code:
    /*
       Compress a string into compString
       then decopmress it into thirdString
       using the zlib1.dll
    
    */
    
    char* returnString = "hello world";
    char* compString;
    char* thirdString;
    int rv;
    
    Action()
    {
    	if ((compString = (char*) malloc(100 * sizeof(char))) == NULL) { 
    		lr_error_message ("Insufficient memory available"); 
    		return -1; 
    	}
    
    	strcpy(compString, "");
    
    	if ((thirdString = (char*) malloc(100 * sizeof(char))) == NULL) { 
    		lr_error_message ("Insufficient memory available"); 
    		return -1; 
    	}
    
    	strcpy(thirdString, "");
    
    	rv = lr_load_dll("zlib1.dll");
    
    	if(0 == rv)
    		lr_error_message("dll was loaded: return code %d", rv);
    	else
    		lr_error_message("dll was not loaded: return code %d", rv);
    
    	lr_error_message("returnString is %s", returnString);
    	lr_error_message("compString is %s", compString);
    
    	rv = deflate(returnString, compString);
    
    	lr_error_message("returnstring is %s and compString is %s: The dll return code was %d",returnString, compString, rv);
    
    	//inflate(compString, thirdString);
    	lr_error_message("thirdstring is %s", thirdString);
    
    	return 0;
    }
    The structure may be a bit strange as I'm using a tool called Loadrunner which has a C complier built in.

    When I run I get:

    Starting action Action.
    Action.c(33): Error: dll was loaded: return code 0
    Action.c(37): Error: returnString is hello world
    Action.c(38): Error: compString is
    Action.c(42): Error: returnstring is hello world and compString is : The dll return code was -2
    Action.c(45): Error: thirdstring is
    Ending action Action.


    I really don't know what I'm doing here and I can't get much vanilla "C" from the zlib docs.

    Any tips, tricks or clues gratefully received.



    jerry
    Last edited by Salem; 01-19-2011 at 03:51 AM. Reason: Added [code][/code] tags - learn to use them yourself

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > The dll return code was -2
    Read the manual to find out what error code -2 means?
    zlib 1.2.5 Manual
    #define Z_STREAM_ERROR (-2)

    Reading around, you didn't call deflateInit()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    4
    Yep - get you point but there seems to be a difference in using zlib as a library and as a dll. The -2 is fail but the deflateInit:

    rv = deflateInit(compString, -1);
    lr_error_message("The deflateInit return code was %d", rv);


    gives

    Action.c(41): Error: C interpreter run time error: Action.c (41): Error -- Unresolved symbol : deflateInit.
    Action.c(41): Notify: CCI trace: Compiled_code(0): Action()


    which makes me suspect that the dll works differently ?

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    4
    Got the compression side working:

    #include "zlib.h"

    vuser_init()
    {
    const char * buffer = "<RequestList><Request><Target>LookupMgr</Target><Action>Lookup</Action><ProductType>S</ProductType><ProductID>DST</ProductID><ViewName>stmtSubscriptionDetailViewLink </ViewName><EntityName>StmtSubscriptionDetailView</EntityName><InvokedFromField>DailyStmtMasterDates. debitAcctNumAndCcy</InvokedFromField><RecordsPerPage>1000</RecordsPerPage><ResultSetBufferSize>-1</ResultSetBufferSize><SortCols>debtAcctNumAndCcy</SortCols><InvokedFromEntity><DailyStamentMasterGro upVE><entityVersion/><productInstId/><closingAvilBalanceDcFlag/><closingBookBalanceDcFlag/><detailTableName/><openingBalanceDcFlag/><statementDate/><holdAmount/><totalClosingBookBalance/><isGroupRecord/><ccyDummy/><fromDate/><toDate/><StmtBseInfo><acctCcy/><acctId/><prodSubDetId/><stmtNum/><info2AcctOwn/><orgAcctId/><totalDbitCnt/><totalCrditCnt/><acctName/><Provider><id/><name/></Provider><Subscriber><id/><name/></Subscriber><TransIdent><bankRef/><clientRef/><relRef/></TransIdent><WrkFlowState><id/></WrkFlowState><SumCrdit><ccy/><amount/></SumCrdit><SumDebit><ccy/><amount/></SumDebit></StmtBseInfo><ClosingAvilBalance><ccy/><amount/></ClosingAvilBalance><ClosingBookBalance><ccy/><amount>0.000</amount></ClosingBookBalance><OpeningBalance><ccy/><amount/></OpeningBalance></DailyStamentMasterGroupVE></InvokedFromEntity></Request></RequestList>";
    Byte * zipped = (Byte*)calloc((uInt)1000, 1);
    unsigned long zipped_len = 1000;
    int rc = 0;

    lr_load_dll("zlib.dll");

    rc = compress(zipped, &zipped_len, (const Bytef*)buffer,1315);
    lr_error_message("rc = %d, result len=%d\n", rc, zipped_len);




    return 0;
    }


    which returns

    Run-Time Settings file: "P:\Scripts\Sandbox\zlib_script\\default.cfg" [MsgId: MMSG-27141]
    vuser_init.c(13): Error: rc = 0, result len=535
    Ending action vuser_init.


    bit of a nap then I'll play with decompressing again to prove the circle

    jerry

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    It makes me suspect that you haven't read the manual.
    Code:
    typedef struct z_stream_s {
        Bytef    *next_in;  /* next input byte */
        uInt     avail_in;  /* number of bytes available at next_in */
        uLong    total_in;  /* total nb of input bytes read so far */
    
        Bytef    *next_out; /* next output byte should be put there */
        uInt     avail_out; /* remaining free space at next_out */
        uLong    total_out; /* total nb of bytes output so far */
    
        char     *msg;      /* last error message, NULL if no error */
        struct internal_state FAR *state; /* not visible by applications */
    
        alloc_func zalloc;  /* used to allocate the internal state */
        free_func  zfree;   /* used to free the internal state */
        voidpf     opaque;  /* private data object passed to zalloc and zfree */
    
        int     data_type;  /* best guess about the data type: binary or text */
        uLong   adler;      /* adler32 value of the uncompressed data */
        uLong   reserved;   /* reserved for future use */
    } z_stream;
    
    typedef z_stream FAR *z_streamp;
    A zstream object is much more complicated than simply passing the start of the string you want to compress.

    That aside, have you looked through the zlib.h you're using to see if deflateInit() is even declared somewhere.

    I assume we're talking about the same zlib right - the one from http://www.zlib.net/
    And not some hookey knock-off which just happens to have the same name.


    > which makes me suspect that the dll works differently ?
    One would normally expect to be able to silently switch between .lib and .dll without having to rewrite the code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    4
    Nope - not guilty. The manual is fine for the .lib but a bit sparse on idiots guide to .dll in "C". The zlib comes bundled with Loadrunner from HP so don't know if it's vanilla or if it's been enhanced but the "C" compiler sometimes does get very confused.

    Years since I used a .dll but I remember that there was some way to view any methods ?

    Anyway got it working now - straightforward strings not zstream objects.


    #include "zlib.h"

    const char * buffer = "<RequestList><Request><Target>LookupMgr</Target><Action>Lookup</Action><ProductType>S</ProductType><ProductID>DST</ProductID><ViewName>stmtSubscriptionDetailViewLink </ViewName><EntityName>StmtSubscriptionDetailView</EntityName><InvokedFromField>DailyStmtMasterDates. debitAcctNumAndCcy</InvokedFromField><RecordsPerPage>1000</RecordsPerPage><ResultSetBufferSize>-1</ResultSetBufferSize><SortCols>debtAcctNumAndCcy</SortCols><InvokedFromEntity><DailyStamentMasterGro upVE><entityVersion/><productInstId/><closingAvilBalanceDcFlag/><closingBookBalanceDcFlag/><detailTableName/><openingBalanceDcFlag/><statementDate/><holdAmount/><totalClosingBookBalance/><isGroupRecord/><ccyDummy/><fromDate/><toDate/><StmtBseInfo><acctCcy/><acctId/><prodSubDetId/><stmtNum/><info2AcctOwn/><orgAcctId/><totalDbitCnt/><totalCrditCnt/><acctName/><Provider><id/><name/></Provider><Subscriber><id/><name/></Subscriber><TransIdent><bankRef/><clientRef/><relRef/></TransIdent><WrkFlowState><id/></WrkFlowState><SumCrdit><ccy/><amount/></SumCrdit><SumDebit><ccy/><amount/></SumDebit></StmtBseInfo><ClosingAvilBalance><ccy/><amount/></ClosingAvilBalance><ClosingBookBalance><ccy/><amount>0.000</amount></ClosingBookBalance><OpeningBalance><ccy/><amount/></OpeningBalance></DailyStamentMasterGroupVE></InvokedFromEntity></Request></RequestList>";
    unsigned char* returnString;
    unsigned long zipped_len = 1000;
    unsigned long unzipped_len = 1000;
    int rc = 0;

    vuser_init()
    {
    Byte * zipped = (Byte*)calloc((uInt)1000, 1);
    Byte * unzipped = (Byte*)calloc((uInt)10000, 1);

    if ((returnString = (unsigned char*) malloc(10000 * sizeof(char))) == NULL) {
    lr_error_message ("Insufficient memory available");
    return -1;
    }

    lr_load_dll("zlib.dll");

    //compress (dest, destLen, source, sourceLen)
    rc = compress(zipped, &zipped_len, (const Bytef*)buffer,1315);
    lr_error_message("rc = %d, zipped_len = %d\n", rc, zipped_len);

    //uncompress (dest, destLen, source, sourceLen)
    rc = uncompress(returnString, &unzipped_len, (const Bytef*)zipped, 1000);
    lr_error_message("returnString is %s\n", returnString);

    return 0;
    }


    which gives me

    vuser_init.c(23): Error: rc = 0, zipped_len = 535
    vuser_init.c(27): Error: returnString is <RequestList><Request><Target>LookupMgr</Target><Action>Lookup</Action><ProductType>S</ProductType><ProductID>DST</ProductID><ViewName>stmtSubscriptionDetailViewLink </ViewName><EntityName>StmtSubscriptionDetailView</EntityName><InvokedFromField>DailyStmtMasterDates. debitAcctNumAndCcy</InvokedFromField><RecordsPerPage>1000</RecordsPerPage><ResultSetBufferSize>-1</ResultSetBufferSize><SortCols>debtAcctNumAndCcy</SortCols><InvokedFromEntity><DailyStamentMasterGro upVE><entityVersion/><productInstId/><closingAvilBalanceDcFlag/><closingBookBalanceDcFlag/><detailTableName/><openingBalanceDcFlag/><statementDate/><holdAmount/><totalClosingBookBalance/><isGroupRecord/><ccyDummy/><fromDate/><toDate/><StmtBseInfo><acctCcy/><acctId/><prodSubDetId/><stmtNum/><info2AcctOwn/><orgAcctId/><totalDbitCnt/><totalCrditCnt/><acctName/><Provider><id/><name/></Provider><Subscriber><id/><name/></Subscriber><TransIdent><bankRef/><clientRef/><relRef/></TransIdent><WrkFlowState><id/></WrkFlowState><SumCrdit><ccy/>
    Ending action vuser_init.


    so that can go into the pocket in case the client decides they want to compress web service uploads of the data...hopefully not

    jerry

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Yeah, can you please use the [code][/code] tags when posting code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My zlib1.dll wrapper no does not work in .net 4.0
    By theoobe in forum C# Programming
    Replies: 7
    Last Post: 05-04-2011, 08:58 AM