A strange question, I know.

This is a discussion on A strange question, I know. within the C++ Programming forums, part of the General Programming Boards category; Now I have done a fair amount of searching on this, however, I am not even sure if they have ...

  1. #1
    uh oh
    Join Date
    Jan 2005
    Location
    Ontario, CA
    Posts
    66

    A strange question, I know.

    Now I have done a fair amount of searching on this, however, I am not even sure if they have a name for what I am attempting to do... so any help in direction would be greatly appreciated, even if you simply provide the search words that might yield some actual results.

    What I am trying to do is regarding the Win32 API (not using MFC), involving the message handling procedure and the UINT message parameter for the call. I am wondering if there was a way to convert the definition (for example) of WM_CREATE in to an actual string that I could change its letters, then once again use the newly created string to produce another definition's value?

    Here's an example of what I am meaning:
    Code:
    LRESULT CALLBACK WinProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
               Convert <message> to character string;
               Update string as appropriate;
               Convert string to (UINT);
               Use its value for appropriate purposes;
    }
    My problem is what is available for converting a UINT value to a string (say "WM_CREATE") that I could edit and then change back to a UINT for other purposes (I am attempting to re-map these definitions to correspond with values more appropriate to array positions that would allow my program to use appropriate processing methods depending on what message is coming in). Is this even a possibility other then creating a list to which can be referenced each time?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I don't think C++ supports converting named values to their string representation (name as in enum, define, or what ever). However, why is the convert to string step necessary? Why can't you convert or map WM_CREATE to (for example) WM_DESTROY directly?

    Edit:
    Unless you are willing to do something like
    Code:
    switch (message) {
        case WM_CREATE: s = "WM_CREATE"; break;
        //etc.
    }
    But that would still be strange.
    Last edited by anon; 01-29-2008 at 02:27 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    These messages are defined as just that - defines! So it's impossible to convert them back to strings. You just have to create a string and do a switch-case and fill the string with the appropriate string.
    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.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    I don't know exactly what the question is, but maybe you could use this to convert an indentifier to a string?
    Code:
    #include <stdio.h>
    #include <string.h>
    #define STRINGIFY(X) (( X ), #X )
    
    int main()
    {
    	char arr[10];
    	int ident;
    
    	strcpy(arr, STRINGIFY(ident));
    	puts(arr);
    
    	return 0;
    }
    Code:
    #include <iostream>
    #include <string>
    #define STRINGIFY(X) (( X ), #X )
    
    int main()
    {
    	std::string arr;
    	int ident;
    
    	arr = STRINGIFY(ident);
    	std::cout << arr << std::endl;
    
    	return 0;
    }
    Last edited by robwhit; 01-29-2008 at 05:53 PM. Reason: make it work

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    That macro doesn't compile, and anyway the OP is not interested in the name of the variable (message), but the name of the value it contains.

    It might be best to post some more context of the question. Why do you need something like that?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    Are we playing C again? We could easily use std::string if that sample would work. I'm unsure how it works.
    arr = STRINGIFY(ident);
    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.

  7. #7
    uh oh
    Join Date
    Jan 2005
    Location
    Ontario, CA
    Posts
    66
    I think there is going to be no easy way to complete this project, at least in the method that I wanted to.

    The purpose of the conversion from UINT to string, then back to UINT was simply for this.

    Code:
    Func* ptrArray[207]; // set to various values as required by the developer
    .
    .
    .
    WndProc() {
               char* string=ConvertDef2String (say WM_CREATE to "WM_CREATE");
               string[1]="W";
               msg=MAKEINTRESOURCE(string); // I believe this is what would do it
               if( *ptrArray[msg] != NULL ) { *ptrArray[msg](params); }
               else { DefWindowProc(params); }
    }
    But now that I have realized that any number of definitions could technically hold the same values as each other, there is no way that there could be a single method of simply identifying which string could possibly match that particular value. So I have resolved to simply creating int getArrayPos(msg), or something of the like that will reference array positions to the possible msg values and return the appropriate array position. Reference List of some kind basically. Unless someone else can think of something? I can't get the STRINGIFY example to compile on my system either.


    PS - Elysia, yes I most likely am ... a hard habit to break. I'm still having issues abandoning #include <stdio.h> and all the functions that come with it.
    Last edited by cyreon; 01-29-2008 at 04:16 PM. Reason: Adding a PS.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    Code:
    WndProc() {
    Doesn't work in C++. All functions must explicitly return something.
    Anyway, what you might want is a map.

    Actually, this sounds a lot like you want to do function pointers for specific messages, does it not?
    Perhaps this thread might help: Creating a function pointer from a button click event?
    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.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    In case people missed it, I fixed the macro.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    You're still writing C code in the C++ forum.
    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.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Macros are the same. The C is just example code. but I'll change it for you.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    ...Except it's not good to post C code in the C++ forum and vice versa, yes?
    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.

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    It's not preferred, agreed.

  14. #14
    uh oh
    Join Date
    Jan 2005
    Location
    Ontario, CA
    Posts
    66
    Elysia,
    I realize that WndProc() doesn't work in C++, it doesn't work in C either. Its a psuedo/code mix just to display what I was attempting to do, in actuality it should look like LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam), but I just placed WndProc() for easier typing. A case of laziness, nothing more.


    Sorry, just realized the last portion of what you typed. Yes I am trying to create an array of function pointers that will allow me to call each repectively. I will have to look in to that, it might just be what I'm looking for. Thanks again!
    Last edited by cyreon; 01-29-2008 at 08:25 PM. Reason: Misread

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307

    Unhappy

    I would be all for using a std::map to convert form one Windows message type ID to another, but not to and from strings.
    That to me seems like an exercize in Softcoding, for which you'd need to read this:
    http://thedailywtf.com/Articles/Soft_Coding.aspx
    http://en.wikipedia.org/wiki/Softcoding
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

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

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 11:13 AM
  3. Strange results using dnsapi and windns
    By Niara in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-13-2005, 10:21 AM
  4. Question type program for beginners
    By Kirdra in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 05:10 AM
  5. Party question
    By Barjor in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 03-18-2002, 02:39 PM

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