Thread: Help a Beginner!

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    3

    Help a Beginner!

    Hey, im making a DLL and i need some help. I need it to return the text in yellow. So what do I add to the following to do so. Here's what i have.

    Code:
    #include "dll.h"
    #include <windows.h>
    #include <iostream.h>
    
    int main()
    {
    
    HANDLE hOut;
    
    hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    
    SetConsoleTextAttribute(hOut,
                                FOREGROUND_RED | 
                                FOREGROUND_GREEN); }
                                
    export double text(double string1, double string2)
      
    {
           return (double) string1;
    }
    Also is it possible and if so, what steps do you take in searching a string for certain characters and words.

    - Thanx

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    what steps do you take in searching a string for certain characters and words.
    strstr searches for a string inside a string; strchr searches for a character; and strtok seperates a string into strings. There are lots of other functions, too: check some documentation.

    So what happened with your DLL code? Did it work? Probably not, or you wouldn't post it here.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    There isn't something like HTML where your output is written to the console as <color=yellow>yourstringhere</color>. SetConsoleTextAttribute changes attributes of the console window itself, so that any output written to the console has that specific attribute (i.e. color). Here's a function I made you could try and elaborate on:

    Code:
    #include <windows.h>
    typedef enum colors 
    { 
    	red = FOREGROUND_RED, 
    	blue = FOREGROUND_BLUE, 
    	green = FOREGROUND_GREEN 
    };
    void WriteColoredString(const char* s, short int color)
    {
    	DWORD dwNumChars;
    	HANDLE hStdout = ::GetStdHandle(STD_OUTPUT_HANDLE);
    	::SetConsoleTextAttribute(hStdout, color);
    
    
    	/*
    	 * std::cout << s;
    	 * printf("%s", s);
    	 */
    
    	// Write string and change it back to white
    	::WriteConsole(hStdout, s, strlen(s), &dwNumChars, 0);
    	::SetConsoleTextAttribute(hStdout, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
    }
    int main(int argc, char *argv[])
    {
    	WriteColoredString("hello\n", red);
    	WriteColoredString("hello\n", blue);
    	WriteColoredString("hello\n", green);
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    3
    Okay, so in the DLL i need it to return text, so with

    Code:
    export double text(double string1, double string2)
    How do i set that to be the colour?

    Code:
    #include "dll.h"
    #include <windows.h>
    #include <iostream.h>
    
    typedef enum colors 
    { 
    	red = FOREGROUND_RED, 
    	blue = FOREGROUND_BLUE, 
    	green = FOREGROUND_GREEN 
    };
    
    void WriteColoredString(const char* s, short int color)
    {
    	DWORD dwNumChars;
    	HANDLE hStdout = ::GetStdHandle(STD_OUTPUT_HANDLE);
    	::SetConsoleTextAttribute(hStdout, color);
    	
    	::WriteConsole(hStdout, s, strlen(s), &dwNumChars, 0);
    	::SetConsoleTextAttribute(hStdout, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
    }
                                
    export double text(double string1, double string2)
      
    {
           return (double) string1;
    }

  5. #5
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by Mad Dawg
    Code:
                
    export double text(double string1, double string2)
      
    {
           return (double) string1;
    }
    why are you passing a string as a floating point number?
    change it to
    Code:
                
    std::string text(std::string& string1, std::string& string2)
      
    {
          // do something useful here
           return string1;
    }
    to start with

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    3
    Ya, but now it doesn't work you see because in the header file is:

    Code:
    #define export extern "C" __declspec (dllexport)
    thats why i had:

    Code:
    export double text(double string1, double string2)
      
    {
           return (double) string1;
    }

  7. #7
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Yes the whole double thing makes no sense. Well, I'm pretty proud with my latest experimentations with all this win32 console programming. The name decoration stumped me for a little while, but I finally got this ugly P.O.S.

    The .DLL module
    Code:
    #define _WIN32_WINNT 0x0600
    #include <windows.h>
    
    
    __declspec(dllexport) char* __cdecl WriteColoredString(const char* s, short int color)
    {
    	DWORD dwNumChars;
    	HANDLE hStdout = ::GetStdHandle(STD_OUTPUT_HANDLE);
    	::SetConsoleTextAttribute(hStdout, color);
    
    
    	/*
    	 * std::cout << s;
    	 * printf("%s", s);
    	 */
    
    	// Write string and change it back to white
    	::WriteConsole(hStdout, s, strlen(s), &dwNumChars, 0);
    	::SetConsoleTextAttribute(hStdout, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
    	return const_cast<char*>(s);
    }
    And the executable that loads it:
    Code:
    #include <windows.h>
    #include <iostream>
    typedef enum colors 
    { 
    	red = FOREGROUND_RED, 
    	blue = FOREGROUND_BLUE, 
    	green = FOREGROUND_GREEN 
    };
    typedef char* (*LPFUNC) (const char*, short int);
    int main(int argc, char *argv[])
    {
    	HINSTANCE hLib;
    	hLib = ::LoadLibrary("C:\\Development\\Dev\\test\\test1\\Debug\\Project1.dll");
    	if(hLib != NULL)
    	{
    		char* (*ourFunc)(const char*, short int) = 
    			reinterpret_cast<char* (*) (const char*, short int)> 
    			(::GetProcAddress(hLib, "_Z18WriteColoredStringPKcs"));
    		if(ourFunc != NULL)
    			(*ourFunc) ("hello", red);
    		else std::cout << "Error loading function from DLL\n";
    	}
    	else 
    		std::cout << "Error loading DLL\n";
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 11:48 AM
  2. What are some good beginner programs I shouold make?
    By oobootsy1 in forum C# Programming
    Replies: 6
    Last Post: 08-09-2005, 02:02 PM
  3. Best Free Beginner Online Books/Tutorials?
    By Zeusbwr in forum C++ Programming
    Replies: 2
    Last Post: 10-12-2004, 05:52 PM
  4. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM