Results 1 to 6 of 6

Thread: [RESOLVED] Porting code from C++

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2005
    Location
    Philadelphia
    Posts
    48

    Resolved [RESOLVED] Porting code from C++

    I've been working on porting code over from C++, I ported most of the code but I can't seem to manage to get part of it done.

    Code:
    IntPtr m_MapFile;
                IntPtr m_MapAddress;
                MMFTextControlMapping m_TCM;
                Queue<MMFTextCommand> m_QueuedCommands;
    
                public CTextHelper(string name)
                {
    
                    m_MapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, true, name);
                    
    
                    if (m_MapFile == null)
                    {
                        return;
                    }
    
                    m_MapAddress = MapViewOfFile(m_MapFile, // handle to mapping object 
                        FILE_MAP_ALL_ACCESS,               // read/write permission 
                        0,                                 // max. object size 
                        0,                                 // size of hFile 
                        0);                                // map entire file 
    
                    if (m_MapAddress == null)
                    {
                        return;
                    }
    
                  m_TCM = (MMFTextControlMapping*)m_MapAddress; // this is the part of the code I can't figure out how to port
                }
    Any help would be appreciated, the line if I can't port is: m_TCM = (MMFTextControlMapping*)m_MapAddress;.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Porting code from C++

    Are you getting an error when porting it over?

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2005
    Location
    Philadelphia
    Posts
    48

    Re: Porting code from C++

    Code:
    Cannot take the address of, get the size of, or declare a pointer to a managed type ('MMFKeyboardControlMapping')

    This is the error I get, MMFKeyboardControlMapping is a struct.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Porting code from C++

    Why do you need a pointer to this object? What do you intend to do with it? If you really need a pointer in your C# code then you must allow unsafe code in your project and create an unsafe block. If you need a pointer for unmanaged code then you can use Marshal.StructureToPtr. Most likely you don't actually need a pointer though, but we really can't say unless we know what you want to do.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    Dec 2005
    Location
    Philadelphia
    Posts
    48

    Re: Porting code from C++

    It's not that I need the pointer, but I have no idea how to do it in C#. The commented code you see there is still C++, I'm trying to use Memory Mapped Files.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Porting code from C++

    Looking at your code more closely, I see that m_MapAddress is an IntPtr. At a guess, I'd say that you should use Marshal.PtrToStructure to turn that IntPtr into a MMFTextControlMapping structure instance. I say that it's a guess because all you've done so far is posted some code and said you can't convert it. You made no attempt to explain the code or what you're actually trying to achieve, so we have to guess and make assumptions.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width