Programming help

Discussion in 'Plugins' started by mikla521, Jan 19, 2012.

  1. mikla521

    mikla521 Registered

    Joined:
    Nov 4, 2011
    Messages:
    14
    Likes Received:
    0
    Hi, I consider myself as quite advanced when it comes to programming in C, C++ and C#, do it for a living.
    But there is a problem I can't solve, been looking around the internet for several days now without success.

    So, I though I would do a plugin somewhat like Realtime Telemetry (or what is was called for SimBin, rF...).
    The thing is that I want to make a GUI in WPF and use shared memory (for now).
    But I do have problems with Marshalling, is there an expert out there who can guide me? :)


    Thanks!
     
  2. bigal

    bigal Registered

    Joined:
    Jan 10, 2012
    Messages:
    11
    Likes Received:
    0
    I did something like this in rF1 worked well not sure if it will help. I would love to try it out if you get it working.

    The dll.

    Code:
    HWND hwndExternalApplication;
    
    char buffer[1024];
    char str[1024];
                
    sprintf(buffer, "%.f,%.f,", info.mUnfilteredThrottle * 100.0f, info.mUnfilteredBrake * 100.0f);
    strcat (str, buffer);
    
    hwndExternalApplication = FindWindow(NULL, "Telemetry"); //Getting the handle for the Application.
    COPYDATASTRUCT cdsData; //The structure used to send data.
    cdsData.dwData = 1000; //Setting an arbitrary value which will tell C# we're sending a string.
    cdsData.cbData = strlen(str); //Wide characters = lenght times two.
    cdsData.lpData = str; //Setting the string to be sent.
    //Sending the data.
    SendMessage(hwndExternalApplication, WM_COPYDATA, (WPARAM)hwndExternalApplication, (LPARAM)&cdsData);
    The Program

    Code:
    public const int WM_COPYDATA = 0x004A;
    IntPtr telemetryData = (IntPtr)1000; //telemetry data
    IntPtr scoringData = (IntPtr)1001; //scoring data
    
    public struct COPYDATASTRUCT
        {
        public IntPtr dwData;
        public int cbData;
        public IntPtr lpData;
        }
    
    protected override void WndProc(ref Message m)
            {
                switch (m.Msg)
                {
                    case WM_COPYDATA:
                        if (m.Msg == WM_COPYDATA)
                        {
                            COPYDATASTRUCT cds = new COPYDATASTRUCT();
                            cds = (COPYDATASTRUCT)Marshal.PtrToStructure(m.LParam,
                             typeof(COPYDATASTRUCT));
    
                            if (cds.dwData == scoringData || cds.dwData == telemetryData)
                            {
    
                                if (cds.cbData > 0)
                                {
                                    byte[] data = new byte[cds.cbData];
                                    Marshal.Copy(cds.lpData, data, 0, cds.cbData);
                                    char[] mChar = Encoding.ASCII.GetChars(data);
                                    string returnText = new string(mChar);
    
                                    m.Result = (IntPtr)1;
    
                                    if (cds.dwData == telemetryData)
                                    {
                                       do something with returnText
                                    }
                                    if (cds.dwData == scoringData)
                                    {
                                       do something with returnText
                                    }
                                    
                                }
                            }
                        }
                        break;
                }
                base.WndProc(ref m);
            }
     
  3. mikla521

    mikla521 Registered

    Joined:
    Nov 4, 2011
    Messages:
    14
    Likes Received:
    0
    Thanks, but no help since both are in C++ :) *Digging deeper*
     
  4. mikla521

    mikla521 Registered

    Joined:
    Nov 4, 2011
    Messages:
    14
    Likes Received:
    0
    Solved it :) There was a mismatch between the structs in C++ and C#.
     
  5. cbo100

    cbo100 Registered

    Joined:
    Jan 22, 2012
    Messages:
    2
    Likes Received:
    0
    Curious (from a not so expert C++ perspective), why you went straight to shared memory?

    I'm doing something similar, as crazy as it sounds, I'm doing an HTML5 gui with JSON via WebSockets for comms. So I'll be able to use my phone/ipad for the display.

    And I investigated various options but went with what I knew (Named Pipes) for the communication between the plugin and my server application. Got the first version working yesterday (simple gear and speed indicator in safari on my ipad) and it's perfectly fine after a few hours testing (no lag or crashes).

    But now I'm wondering now if I should switch to Shared Memory or something else. Does ISI have a recommendation for this sort of thing? Am I likely to run into issues with pipes from my plugin?
     
  6. Mitt Wilson

    Mitt Wilson Registered

    Joined:
    Dec 31, 2010
    Messages:
    306
    Likes Received:
    1
    Amazing what the Human Brain can Achieve if it is actually used for more than thinking about what could have been...
    Back on Topic! Are we going to be able to enjoy you RealTime Telemetry?

    Also do you think you could make a PlugIn that can Replicate RealTime Weather conditions at a Track using the Weather forecast from weather stations?

    Reason- I have started a Server RealTime Weather Races in which I have looked up Actual weather conditions for the exact time and for Mills City,Virgina and Portugal and It was Outstanding - The race started at 8am till 12:pm (gametime 10x) it brought a new level of immersion to the game!!

     
  7. mikla521

    mikla521 Registered

    Joined:
    Nov 4, 2011
    Messages:
    14
    Likes Received:
    0
    Without any testing or any kind of specific science, I decided to go for Shared Memory because it "feels" like it will the quickest way to get updates. I also wanted to learn more about Shared Memory :)
    As soon as everything is working as I want, I will use TCP/IP as an option.

    Are we going to be able to enjoy you RealTime Telemetry? I guess someone else will finish a similar application sooner. So first I am making it for myself, but if I'm satisfied...why not? :)
     

Share This Page