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. Redirect to my struct

Redirect to my struct

Scheduled Pinned Locked Moved C / C++ / MFC
question
18 Posts 7 Posters 1 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.
  • _ _Flaviu

    Inside myheader.h there is a lot for other structs and defines that is in conflict with those from Windows. SID_IDENTIFIER_AUTHORITY for example ... So, removing or renaming is not an option (I guess). But I am not figure out how to solve this conflict with namespacing ...

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #9

    This is just going to lead to more and more problems for you. You need to isolate your definitions from the ones in Windows. You can do it with the use of namespaces, or changing the names in your local definitions. But either way, until you fix this issue you are wasting your time trying to build any of the code.

    _ 1 Reply Last reply
    0
    • L Lost User

      This is just going to lead to more and more problems for you. You need to isolate your definitions from the ones in Windows. You can do it with the use of namespaces, or changing the names in your local definitions. But either way, until you fix this issue you are wasting your time trying to build any of the code.

      _ Offline
      _ Offline
      _Flaviu
      wrote on last edited by
      #10

      Thank you Richard. "You can do it with the use of namespaces" Can you give me a little pseudocode that show me how to do it ?

      K L 2 Replies Last reply
      0
      • _ _Flaviu

        Thank you Richard. "You can do it with the use of namespaces" Can you give me a little pseudocode that show me how to do it ?

        K Offline
        K Offline
        k5054
        wrote on last edited by
        #11

        Namespaces are a C++ feature, and not available if you are using C only. Basically a namespace wraps a set of definitions so that they can be uniquely identified within a source listing. eg:

        namespace MyLib {
        class A {
        int x;
        ...
        };
        }

        namespace MyProj {
        class A {
        int y;
        ...
        };
        }

        ...

        int main() {
        MyProj::A a1; // defines an object of type class A from namespace MyProj
        MyLib::A a2; // defines an object of type class A from namespace MyLib

        a1.y = a2.y;   // don't need namespace resolution tags within function
        

        }

        **update:**The last line of the sample code, above should be a1.y = a2.x, apologies for any confusion :( If, however, you are using C, you've got two choices. 1) refactor you project so that your SID and the system SID never appear in the same source code file (otherwise known as a translation unit). That's not always possible, and even when it is, it's usually requires a lot of thought, care, and effort. 2) refactor you project and rename your SID to something else, eg MyProg_SID. That's still a lot of work, but its' probably a lot less work than option 1, above. Additionally, your IDE might provide a renaming tool that will do the grunt work for you. If not, you might be able to use the find and replace feature of your favorite text editor.

        _ 1 Reply Last reply
        0
        • _ _Flaviu

          Thank you Richard. "You can do it with the use of namespaces" Can you give me a little pseudocode that show me how to do it ?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #12

          It is not that straightforward unfortunately. A namespace is a qualifier that allows you to have objects with the same name in a single compilation unit. As a simple example:

          // NOTE: namespaces must be declared at file scope, or in a header

          namespace foo
          {
          typedef struct
          {
          int bar;
          } FOOBAR;
          };

          namespace gaa
          {
          typedef struct
          {
          char bar;
          } FOOBAR;
          };

          // the struct name FOOBAR may now be qualified to avoid ambiguity

          int main
          {
          foo::FOOBAR foobar; // as declared in namespace foo
          foobar.bar = 10; // the variable is int type

          gaa::FOOBAR gaabar;  // as declared in namespace gaa
          gaabar.bar = 'X';    // here is is char type
          

          }

          1 Reply Last reply
          0
          • _ _Flaviu

            I have some code:

            sid->identifier_authority

            where sid is declared as:

            const SID* sid

            and the project say that sid is targeting to windows (WinNT.h):

            typedef struct _SID {
            ...
            } SID, *PISID;

            but I also have (in some header file) inside my project another SID struct declared:

            #ifdef SID
            typedef struct {
            u8 sub_authority_count;
            SID_IDENTIFIER_AUTHORITY identifier_authority;
            }SID;
            #endif

            how can I do to sid be as my SID struct, not as _SID windows struct ?

            K Offline
            K Offline
            k5054
            wrote on last edited by
            #13

            This doesn't do what you think it does:

            typedef struct_SID {
            ...
            } SID, *PISID

            #ifdef SID
            typedef struct {
            ...
            }SID;
            #endif

            #ifdef,#undef etc only refers to preprocessor tokens. In C/C++, you can not undefine (or redefine) a previously defined item. e.g. the following is not valid

            int x;
            #ifdef x // only if preprocessor token 'x' exists
            #undef x // doesn't undefine int x
            struct { // compiler error if branch taken, redifines type of x
            int i;
            double d;
            } x;
            #endif

            ...
            x myX; //compiler error: typeof x unknown

            _ 1 Reply Last reply
            0
            • _ _Flaviu

              Inside myheader.h there is a lot for other structs and defines that is in conflict with those from Windows. SID_IDENTIFIER_AUTHORITY for example ... So, removing or renaming is not an option (I guess). But I am not figure out how to solve this conflict with namespacing ...

              L Offline
              L Offline
              leon de boer
              wrote on last edited by
              #14

              The field names are not an issue you just need the struct, typedef and pointer to have different names As Richard said it's going to bite you find a different name. If you want to try a different name with minimal typing go to your file myheader.h and at the top after the guard put these

              #define _SID _mySID
              #define SID mySID
              #define PISID myPISID

              now go to the very bottom and put these

              #undef _SID
              #undef SID
              #undef PISID

              Now your SID is called mySID to all other units because it just does a text substitute :-) If you are happy it is all well the make the change permanently using Edit->find & replace on the file using Visual Studio.

              In vino veritas

              _ 1 Reply Last reply
              0
              • L leon de boer

                The field names are not an issue you just need the struct, typedef and pointer to have different names As Richard said it's going to bite you find a different name. If you want to try a different name with minimal typing go to your file myheader.h and at the top after the guard put these

                #define _SID _mySID
                #define SID mySID
                #define PISID myPISID

                now go to the very bottom and put these

                #undef _SID
                #undef SID
                #undef PISID

                Now your SID is called mySID to all other units because it just does a text substitute :-) If you are happy it is all well the make the change permanently using Edit->find & replace on the file using Visual Studio.

                In vino veritas

                _ Offline
                _ Offline
                _Flaviu
                wrote on last edited by
                #15

                Interesting idea, and it works for this case, but inside that myheader.h, I have several structs with names as windows have ... so, I guess is not productive renaming structs ... that is why I am thinking seriously at namespaces ... but I don't know how to use it yet.

                1 Reply Last reply
                0
                • K k5054

                  This doesn't do what you think it does:

                  typedef struct_SID {
                  ...
                  } SID, *PISID

                  #ifdef SID
                  typedef struct {
                  ...
                  }SID;
                  #endif

                  #ifdef,#undef etc only refers to preprocessor tokens. In C/C++, you can not undefine (or redefine) a previously defined item. e.g. the following is not valid

                  int x;
                  #ifdef x // only if preprocessor token 'x' exists
                  #undef x // doesn't undefine int x
                  struct { // compiler error if branch taken, redifines type of x
                  int i;
                  double d;
                  } x;
                  #endif

                  ...
                  x myX; //compiler error: typeof x unknown

                  _ Offline
                  _ Offline
                  _Flaviu
                  wrote on last edited by
                  #16

                  I guess I know that. In WinNT.h I got:

                  typedef struct _SID {
                  ....
                  } SID, *PISID;

                  and in myheader.h I got:

                  #ifdef SID
                  typedef struct {
                  ...
                  }SID;
                  #endif

                  and my project take SID from WinNT.h, and I want to take it from myheader.h. Even if I get out #ifdef SID and #endif from myheader.h, the situation is the same. And moreover, inside myheader.h I have several struct with the same naming as windows has.

                  1 Reply Last reply
                  0
                  • K k5054

                    Namespaces are a C++ feature, and not available if you are using C only. Basically a namespace wraps a set of definitions so that they can be uniquely identified within a source listing. eg:

                    namespace MyLib {
                    class A {
                    int x;
                    ...
                    };
                    }

                    namespace MyProj {
                    class A {
                    int y;
                    ...
                    };
                    }

                    ...

                    int main() {
                    MyProj::A a1; // defines an object of type class A from namespace MyProj
                    MyLib::A a2; // defines an object of type class A from namespace MyLib

                    a1.y = a2.y;   // don't need namespace resolution tags within function
                    

                    }

                    **update:**The last line of the sample code, above should be a1.y = a2.x, apologies for any confusion :( If, however, you are using C, you've got two choices. 1) refactor you project so that your SID and the system SID never appear in the same source code file (otherwise known as a translation unit). That's not always possible, and even when it is, it's usually requires a lot of thought, care, and effort. 2) refactor you project and rename your SID to something else, eg MyProg_SID. That's still a lot of work, but its' probably a lot less work than option 1, above. Additionally, your IDE might provide a renaming tool that will do the grunt work for you. If not, you might be able to use the find and replace feature of your favorite text editor.

                    _ Offline
                    _ Offline
                    _Flaviu
                    wrote on last edited by
                    #17

                    That C code I modified as C++ code, so I could use namespaces :)

                    1 Reply Last reply
                    0
                    • _ _Flaviu

                      I have some code:

                      sid->identifier_authority

                      where sid is declared as:

                      const SID* sid

                      and the project say that sid is targeting to windows (WinNT.h):

                      typedef struct _SID {
                      ...
                      } SID, *PISID;

                      but I also have (in some header file) inside my project another SID struct declared:

                      #ifdef SID
                      typedef struct {
                      u8 sub_authority_count;
                      SID_IDENTIFIER_AUTHORITY identifier_authority;
                      }SID;
                      #endif

                      how can I do to sid be as my SID struct, not as _SID windows struct ?

                      S Offline
                      S Offline
                      Stefan_Lang
                      wrote on last edited by
                      #18

                      _Flaviu wrote:

                      #ifdef SID
                      typedef ... SID;

                      Sorry to be rude, but that's bullsh1t! If there really is a #define for the symbol SID anywhere in your code or your precompiler options, then your ccode will most likely never compile, because any attempt to use, declare or otherwise reference a struct SID will be turned into garbage by the precompiler which replaces the symbol with something else! So, unless and until you make sure that nobody does such a #define, there is no point looking further! And then, of course, the #ifdef makes no sense - not that it did before.

                      GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                      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