Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Typedef and more

Typedef and more

Scheduled Pinned Locked Moved C / C++ / MFC
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NicholasCougar
    wrote on last edited by
    #1

    Hi: I encount such codes, plz explain them in detail. Thank you in advance.:rose: typedef unsigned (WINAPI *PBEGINTHREADEX_THREADFUNC)(LPVOID lpThreadParameter); typedef unsigned *PBEGINTHREADEX_THREADID; Best regard. I confess that I am a stubborn guy, but why not put things thoroughly, logically and systematically clean. One concrete prolem is worth a thousand unapplied abstractions.

    C R L 3 Replies Last reply
    0
    • N NicholasCougar

      Hi: I encount such codes, plz explain them in detail. Thank you in advance.:rose: typedef unsigned (WINAPI *PBEGINTHREADEX_THREADFUNC)(LPVOID lpThreadParameter); typedef unsigned *PBEGINTHREADEX_THREADID; Best regard. I confess that I am a stubborn guy, but why not put things thoroughly, logically and systematically clean. One concrete prolem is worth a thousand unapplied abstractions.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      A typedef allows a new parameter to be created, it's more intelligent than a macro ( I believe ) but does the same sort of thing in effect. In this case, for example, PBEGINTHREADEX_THREADID is the same as an unsigned (int) pointer. The reason to do this is that the type you use through your code for something specific can be changed just by changing the typedef. I've never found a use for them, so if anyone else contradicts me, believe them first :-) Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm somewhat suspicious of STL though. My (test,experimental) program worked first time. Whats that all about??!?! - Jon Hulatt, 22/3/2002

      R R 2 Replies Last reply
      0
      • N NicholasCougar

        Hi: I encount such codes, plz explain them in detail. Thank you in advance.:rose: typedef unsigned (WINAPI *PBEGINTHREADEX_THREADFUNC)(LPVOID lpThreadParameter); typedef unsigned *PBEGINTHREADEX_THREADID; Best regard. I confess that I am a stubborn guy, but why not put things thoroughly, logically and systematically clean. One concrete prolem is worth a thousand unapplied abstractions.

        R Offline
        R Offline
        Rickard Andersson20
        wrote on last edited by
        #3

        NicholasCougar wrote: typedef unsigned (WINAPI *PBEGINTHREADEX_THREADFUNC)(LPVOID lpThreadParameter); function pointer NicholasCougar wrote: typedef unsigned *PBEGINTHREADEX_THREADID; a typedef of a unsigned! :) A typedef is a way to make your own names of types in C++. And sometimes "makeing" your own types... ------------------------------------ Rickard Andersson, Suza Computing ICQ#: 50302279 I'm from the winter country SWEDEN! ------------------------------------

        1 Reply Last reply
        0
        • C Christian Graus

          A typedef allows a new parameter to be created, it's more intelligent than a macro ( I believe ) but does the same sort of thing in effect. In this case, for example, PBEGINTHREADEX_THREADID is the same as an unsigned (int) pointer. The reason to do this is that the type you use through your code for something specific can be changed just by changing the typedef. I've never found a use for them, so if anyone else contradicts me, believe them first :-) Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm somewhat suspicious of STL though. My (test,experimental) program worked first time. Whats that all about??!?! - Jon Hulatt, 22/3/2002

          R Offline
          R Offline
          Rickard Andersson20
          wrote on last edited by
          #4

          so my answer could be wrong? :-O ------------------------------------ Rickard Andersson, Suza Computing ICQ#: 50302279 I'm from the winter country SWEDEN! ------------------------------------

          C 1 Reply Last reply
          0
          • N NicholasCougar

            Hi: I encount such codes, plz explain them in detail. Thank you in advance.:rose: typedef unsigned (WINAPI *PBEGINTHREADEX_THREADFUNC)(LPVOID lpThreadParameter); typedef unsigned *PBEGINTHREADEX_THREADID; Best regard. I confess that I am a stubborn guy, but why not put things thoroughly, logically and systematically clean. One concrete prolem is worth a thousand unapplied abstractions.

            L Offline
            L Offline
            Lakitu
            wrote on last edited by
            #5

            Hi, the first defines a pointer to a function that takes a void pointer as parameter and returns an unsigned. The second simply defines PBEGINTHREADEX_THREADID to be a pointer to an unsigned. You probably got this from the CreateThread API, right? ;)


            You know, for kids!

            N 1 Reply Last reply
            0
            • R Rickard Andersson20

              so my answer could be wrong? :-O ------------------------------------ Rickard Andersson, Suza Computing ICQ#: 50302279 I'm from the winter country SWEDEN! ------------------------------------

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              I thought my answer might be a little more expansive, but I didn't think yours was wrong. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm somewhat suspicious of STL though. My (test,experimental) program worked first time. Whats that all about??!?! - Jon Hulatt, 22/3/2002

              R 1 Reply Last reply
              0
              • C Christian Graus

                I thought my answer might be a little more expansive, but I didn't think yours was wrong. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm somewhat suspicious of STL though. My (test,experimental) program worked first time. Whats that all about??!?! - Jon Hulatt, 22/3/2002

                R Offline
                R Offline
                Rickard Andersson20
                wrote on last edited by
                #7

                Of course! :rolleyes: And I just wanted to know if I was wrong about that point! :) ------------------------------------ Rickard Andersson, Suza Computing ICQ#: 50302279 I'm from the winter country SWEDEN! ------------------------------------

                1 Reply Last reply
                0
                • C Christian Graus

                  A typedef allows a new parameter to be created, it's more intelligent than a macro ( I believe ) but does the same sort of thing in effect. In this case, for example, PBEGINTHREADEX_THREADID is the same as an unsigned (int) pointer. The reason to do this is that the type you use through your code for something specific can be changed just by changing the typedef. I've never found a use for them, so if anyone else contradicts me, believe them first :-) Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm somewhat suspicious of STL though. My (test,experimental) program worked first time. Whats that all about??!?! - Jon Hulatt, 22/3/2002

                  R Offline
                  R Offline
                  Roger Allen
                  wrote on last edited by
                  #8

                  I have used this kind of typedef in the past. Usually when I have loaded a DLL dynamically, and you nned to store function pointers to the return calls from GetProcAddress. This allows you to save the pointers in member vars and call them like function of a regular class once the pointers are correctly initialised. For example:

                  typedef int (*DLLINIT)(int x);
                  typedef void (*DLLEXIT)() ;

                  class DLLWrapper
                  {
                  DLLINIT DLLInit;
                  DLLEXIT DLLExit ;
                  HINSTANCE hInstance ;

                  DLLWrapper(CString& filename)
                  	{
                  	DLLInit = NULL ;
                  	DLLExit = NULL ;
                  	hInstance = NULL ;
                  	LoadDll(filename) ;
                  	} ;
                  ~DLLWrapper()
                  	{
                  	if (hInstance)
                  		FreeLibrary(hIstance) ;
                  	hInsatnce = NULL ;
                  	}
                  
                  bool		LoadDLL(CString& filename)
                  {
                  hInstance = LoadLibrary(filename);
                  if (!hInstance)
                  	return false ;
                  
                  DLLInit = (DLLINIT)GetProcAddress(RefinementDLL.pDLL, "DLLInit");
                  DLLExit = (DLLEXIT)GetProcAddress(RefinementDLL.pDLL, "DLLExit");
                  return true ;
                  }
                  

                  } ;

                  // you can use the object like this
                  DLLWrapper fred("Some.DLL") ;

                  fred.DLLInit(1) ; // call through typedef pointer

                  That lot was put together quickly, so it may not all be 100% correct. Roger Allen Sonork 100.10016 If I'm not breathing, I'm either dead or holding my breath. A fool jabbers, while a wise man listens. But is he so wise to listen to the fool?

                  1 Reply Last reply
                  0
                  • L Lakitu

                    Hi, the first defines a pointer to a function that takes a void pointer as parameter and returns an unsigned. The second simply defines PBEGINTHREADEX_THREADID to be a pointer to an unsigned. You probably got this from the CreateThread API, right? ;)


                    You know, for kids!

                    N Offline
                    N Offline
                    NicholasCougar
                    wrote on last edited by
                    #9

                    The codes are from Multithreading Applications in Win32 written by Jim Beveridge & Robert Wiener First, please study the second and sixth parameters. unsigned long _beginthreadex( void *security, unsigned stack_size, unsigned (* start_address)(void *), void *arglist, unsigned initflag, unsigned *thrdaddr); HANDLE CreateThread( LPSECURITY_ATTRIBUTES lpThreadAttributes, DWORD dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, DWORD dwCreationFlags, LPDWROD lpThreadId); According to Jim Beveridge & Robert Wiener' point of view, the function _beginthreadex() is the coat of CreateThread() and the types of its parameters have been changed for the sake of being adoptable to other operation system. While, as the CloseHandle() must be called at last, programmers can't get rid of "window.h". Another side effect is, though C compiler makes no difference between DWORD and unsigned (in fact unsigned int), C++ compiler doen't think so. As CreateThread() is inside _beginthreadex(), its parameters are less likely to be modified. Defining parameters according to CreateThread() is smarter. Since the parameters must be accepted by _beginthreadex(), the codes we discussing must appear there. The true meaning of the codes is: force type convertion before calling _beginthreadex(). the following are codes in all: //The beginning of codes #define WIN32_LEAN_AND_MEAN #include #include #include #include typedef unsigned (WINAPI *PBEGINTHREADEX_THREADFUNC)( LPVOID lpThreadParameter ); typedef unsigned *PBEGINTHREADEX_THREADID; int main() { HANDLE hThread; DWORD dwThreadId; int i=0; hThread = (HANDLE) _beginthreadex(NULL, 0, (PBEGINTHREADEX_THREADFUNC)ThreadFunc, // Attention, Plz (LPVOID) i, 0, (PBEGINTHREADEX_THREADID) & dwThreadId // Attention, Plz ); if(hThread) { WaitForSingleObject(hThread, INFINITE); CloseHandle(hThread); } return EXIT_SUCCESS; } DWORD WINAPI ThreadFunc(LPVOID n) { // Do something... return 0; } //The end of codes My opinion

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups