DLL issue

This is a discussion on DLL issue within the C++ Programming forums, part of the General Programming Boards category; Let me start by saying that I知 a total beginner when it comes to C++. Recently I致e created a .dll ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    10

    DLL issue

    Let me start by saying that I知 a total beginner when it comes to C++. Recently I致e created a .dll for a project I知 working on that suppose to XOR each character in a string and then out puts the hex value of the answer... Here's my code...


    Code:
    #include "stdafx.h"
    #include <stdio.h> // for namespace
    #include <valarray> // for Xor 
    #include <string>
    
    BOOL APIENTRY DllMain( HANDLE hModule, 
    DWORD ul_reason_for_call, 
    LPVOID lpReserved
    )
    {
    return TRUE;
    }
    
    char outvalue[2] = "";
    char out [2] = "";
    
    __declspec(dllexport) LPCTSTR ecc(LPCTSTR invalue)
    {
    unsigned int v=0;
    unsigned int vlen=0;
    
    // Setup Values of Variables 
    vlen = strlen(invalue);
    outvalue[0] = invalue[0];
    
    // Loop
    for(v;v<vlen-1;v++)
    { 
    outvalue [0] ^= invalue[v+1]; 
    }
    sprintf(out, "%X",2, outvalue[0]);
    
    return out;
    }

    Now, it's not the prettiest but it works. The only trouble I知 having is that the program I知 using has specific requirements for it to work correctly (right now after the DLL run the program tries to clear the memory but sometimes clears it incorrectly and I知 getting thing crashing because of it).
    The requirements are...

    accepts a constant char* null terminated string as the sole parameter

    returns type char* that points to a dynamically allocated null terminated string that can be freed by the step

    At this point I really could use all the help I can get. Anyone have any ideas how I can patch my current DLL to meet those two steps?





    Thanks a bunch this forum rocks!

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    For reference for others:
    Code:
    #include "stdafx.h"
    #include <stdio.h> // for namespace
    #include <valarray> // for Xor 
    #include <string>
    
    BOOL APIENTRY DllMain( HANDLE hModule, 
    					  DWORD ul_reason_for_call, 
    					  LPVOID lpReserved
    					  )
    {
    	return TRUE;
    }
    
    char outvalue[2] = "";
    char out [2] = "";
    
    __declspec(dllexport) LPCTSTR ecc(LPCTSTR invalue)
    {
    	unsigned int v=0;
    	unsigned int vlen=0;
    
    	// Setup Values of Variables 
    	vlen = strlen(invalue);
    	outvalue[0] = invalue[0];
    
    	// Loop
    	for(v;v<vlen-1;v++)
    	{ 
    		outvalue [0] ^= invalue[v+1]; 
    	}
    	sprintf(out, "&#37;X",2, outvalue[0]);
    
    	return out;
    }
    As a first suggestion, I'd like to advise to get rid of LPCTSTR and the like. In your functions, use appropriate type.
    The function should take const char* and return char*. OK? Good.
    Then you will need to allocate a buffer large enough to hold the new data via new.
    Then you simply assign the XORed data to your new buffer and return it.

    Start with that.
    Last edited by Elysia; 04-02-2008 at 12:15 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DLL Load issue
    By George2 in forum Windows Programming
    Replies: 2
    Last Post: 09-01-2007, 03:42 PM
  2. dll issue
    By axr0284 in forum C++ Programming
    Replies: 1
    Last Post: 03-23-2006, 07:37 AM
  3. dll communicating between each other
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 06-17-2005, 02:20 AM
  4. dll : design issue!
    By dug in forum C++ Programming
    Replies: 8
    Last Post: 02-17-2005, 10:48 AM
  5. DLL and std::string woes!
    By Magos in forum C++ Programming
    Replies: 7
    Last Post: 09-08-2004, 12:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21