Help with simple console to dll code

This is a discussion on Help with simple console to dll code within the C++ Programming forums, part of the General Programming Boards category; I hope someone can help. I知 new to Visual C++ 2008 and I just need help converting a program I ...

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

    Help with simple console to dll code

    I hope someone can help. I知 new to Visual C++ 2008 and I just need help converting a program I wrote to a dll. Here's the program...

    Code:
    #include "stdafx.h"
    #include <stdio.h>			
    #include <tchar.h>
    #include <iostream>			
    #include <valarray>
    			
    using namespace std;
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    // Define Variables
    	char invalue[850] = "";
    	char outvalue[] = "";
    	unsigned int v=0;
    	unsigned int vlen=0;
    	
    
    // Get in value		
    	cin >> invalue;
    
    
    // Setup Values of Variables 
    	vlen = strlen(invalue);
    	outvalue[0] = invalue[0];
    	
    // Loop
        for(v;v<vlen-1;v++)
        {						
    	outvalue [0] ^=  invalue[v+1];   
    	
        }
    
    // Prints results
    	printf ("%x \n", outvalue[0]); 
    
      
      return 0;

    Basically it takes a sting like "AS0100012178 2178" and XOR's each character then takes the result and displays the hex value of the result (in this case should be 1e). In the dll though, i'll need it to output the hex value to the program i'm using. I've tried for two weeks and can't seem to figure out how a dll works completely.

    Anyone think they can help me?! It would be greatly appreciated as I知 pulling my hair out!

    Thanks!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Well the first step would be to write the code you have as a function which does exactly what you want.

    Then I would create a new empty DLL project and simply copy/paste the completed function in step 1 into the newly created DLL template 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    10
    I've tried but I don't understand how to export the results to an external program. That and from what little I understand you need to return the info I need thru the return command, not the print command right?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by crazybucky View Post
    I've tried but I don't understand how to export the results to an external program. That and from what little I understand you need to return the info I need thru the return command, not the print command right?
    Yes, you definitely need to return it through a return value [using the return keyword].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    10
    So in that case does anyone know how to assign the 2 digit hex value of a character to a variable?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    As far as I see it, the output should be an "unsigned char" - you can then print that as a hex-value if you wish, but it's a "small integer" as far as calculations are concerned.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,536
    There are several ways of exporting functions. The most common one is a .def file. Just place the function names under the exports section.
    Otherwise you can use __declspec(dllexport) and __declspec(dllimport) to export/import (Visual Studio specific afaik).
    Then to access the functions in the app, you need to link to the dll .lib or add a reference under the solution to your dll.
    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.

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    10
    Anyone still know how to return a HEX value then?

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,536
    You can return an std::string or an integer from the function in the dll as normal.
    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.

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

    Angry

    Any examples? Sorry to be suck a pain!

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What, an example of how to return an integer?

    Code:
    int sample() {
        return (0x1e);
    }
    The result you have is technically a char (outvalue[0]), so you can return a char and printf it with %x in your other program the same way.

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    10
    The other program I am using I'm not acually writing, so it can't convert, say the char of "N" to a hex value of "4e". So i'll need the dll to do that. The main trouble I having still is I still don't know how to setup the export correctly. The program i'm using asks for just the location of the dll, function name to call, function parameters and then where to send the results.

    I've named my project ecc and create an export.def with the following...

    Code:
    LIBRARY ecc
    EXPORTS
         ecc
    but I don't know how to recieve the string in correctly...

    Code:
    __declspec(dllexport)char invalue[850]

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    My Hat of Guessing thinks you mean
    Code:
    __declspec(dllexport)char ecc(char invalue[850]) {
    // function goes here
    }
    but isn't entirely sure if this is what you're asking. You are exporting a function named ecc? (I've never used declspec et al, so I have no idea, but I'm pretty sure you need to be exporting some kind of function, and why not let that function take a parameter?)

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,536
    IF you use dllexport, you need dllimport as well:
    Code:
    __declspec(dllimport) char ecc(char invalue[850])
    Typically it's defined as a macro in a header.
    Anyhow, if you just put the function names in the .def file under the exports section, the functions will be exported. Then all you need to do is add a reference to the dll project from your application project under solution settings (not project settings) and include the proper header and you can call all functions as if they were local functions within the application.

    From what I see, you just need to export and call a function that returns a value for the main exe to use. With the above method, all you need to do is treat the function as normal, as if it was a local function. Do as you would if it were a local function. Unless you don't know how return values works?
    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.

  15. #15
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Quote Originally Posted by crazybucky View Post
    Any examples? Sorry to be suck a pain!
    The DLL

    Code:
    // myDLL.cpp
    // Compile: cl.exe /LD myDLL.cpp
    
    #include <string.h>
    
    #define DECLARE_EXPORT __declspec(dllexport)
    
    extern "C"
    {
        DECLARE_EXPORT void myFunction( char* pIn, unsigned char * pOut )
        {
    	int v, vlen;	
    	
    	vlen = strlen(pIn);
    	   for(v = 0; v < vlen; v++)
    	pOut[v] =  pIn[v] + 1;   
        }
    }
    The calling code

    Code:
    // Example of explicit linking of DLL
    #include <windows.h>
    #include <stdio.h>
    
    // Function pointer
    typedef void (*myFunction)(char *, unsigned char *);
    
    int main(void)
    {
    	myFunction _myFunction;
    	char cInput[] = {"AS0100012178 2178"};
    	unsigned char ucReturn[24] = {0};
    	int iIndex;
    	HINSTANCE hInstLibrary = LoadLibrary("myDLL.dll");
    
    	if (hInstLibrary)
    	{
    		_myFunction = (myFunction)GetProcAddress(hInstLibrary, "myFunction");
    		if (_myFunction)
    		{
    			 _myFunction(cInput, ucReturn);
    			for(iIndex = 0; iIndex < strlen(cInput); iIndex++)
    				printf("%c         %c  0x%02X\n", cInput[iIndex], ucReturn[iIndex],ucReturn[iIndex]);
    		}
    		else printf("_myFunction failed to initialize\n");
    		FreeLibrary(hInstLibrary);
    	}
    	else printf("myDLL.dll failed To Load!\n");
    	return 0;
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating a simple DLL
    By Dark_Phoenix in forum C++ Programming
    Replies: 8
    Last Post: 03-07-2009, 08:41 PM
  2. Problems with a simple console game
    By DZeek in forum C++ Programming
    Replies: 9
    Last Post: 03-06-2005, 01:02 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Creating a Simple DLL in C
    By Ultima4701 in forum C Programming
    Replies: 2
    Last Post: 11-23-2002, 12:01 PM
  5. Replies: 1
    Last Post: 01-29-2002, 01:49 AM

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