CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2009
    Posts
    3

    how to use #pragma data_seq

    I should write a Windows32 form, in which I can input Unicode. When I create multiple instances of application.all the form must display some content, and When I change the content, the content in all instances must change according to the changed Unicode.
    Now I can get the content of input, but how can I put the value into the variable in commen memory area?

    #include "stdafx.h"
    #include "windows.h"
    #include <memory.h>
    #include <TCHAR.h>
    //#pragma once

    #pragma data_seq("Shared")
    volatile wchar_t* content;
    #pragma data_seq()
    #pragma comment(linker, "/Section:Shared,RWS")


    namespace ShareMemory2 {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System:ata;
    using namespace System:rawing;

    /// <summary>
    /// Summary for Form1
    ///
    /// WARNING: If you change the name of this class, you will need to change the
    /// 'Resource File Name' property for the managed resource compiler tool
    /// associated with all .resx files this class depends on. Otherwise,
    /// the designers will not be able to interact properly with localized
    /// resources associated with this form.
    /// </summary>
    public ref class Form1 : public System::Windows::Forms::Form
    {
    public:
    Form1(void)
    {
    InitializeComponent();
    //
    //TODO: Add the constructor code here
    //
    }

    protected:
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    ~Form1()
    {
    if (components)
    {
    delete components;
    }
    }
    private: System::Windows::Forms::TextBox^ textBox1;
    private: System::Windows::Forms::Button^ button1;
    protected:

    private:
    /// <summary>
    /// Required designer variable.
    /// </summary>
    System::ComponentModel::Container ^components;

    #pragma region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    void InitializeComponent(void)
    {
    this->textBox1 = (gcnew System::Windows::Forms::TextBox());
    this->button1 = (gcnew System::Windows::Forms::Button());
    this->SuspendLayout();
    //
    // textBox1
    //
    this->textBox1->Location = System:rawing::Point(67, 26);
    this->textBox1->Name = L"textBox1";
    this->textBox1->Size = System:rawing::Size(164, 20);
    this->textBox1->TabIndex = 0;
    //
    // button1
    //
    this->button1->Location = System:rawing::Point(105, 64);
    this->button1->Name = L"button1";
    this->button1->Size = System:rawing::Size(75, 23);
    this->button1->TabIndex = 1;
    this->button1->Text = L"button1";
    this->button1->UseVisualStyleBackColor = true;
    this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
    //
    // Form1
    //
    this->AutoScaleDimensions = System:rawing::SizeF(6, 13);
    this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
    this->ClientSize = System:rawing::Size(296, 111);
    this->Controls->Add(this->button1);
    this->Controls->Add(this->textBox1);
    this->Name = L"Form1";
    this->Text = L"Form1";
    this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
    this->ResumeLayout(false);
    this->PerformLayout();

    }
    #pragma endregion

    private : System::String^ Pwchar_tToString(wchar_t* content){
    System::IntPtr ptr(content);
    System::String^ string = System::Runtime::InteropServices::Marshal::PtrToStringUni(ptr);
    return string;
    };

    private : wchar_t *StringToPwchar_t(System::String^ str){
    IntPtr ptr = System::Runtime::InteropServices::Marshal::StringToHGlobalUni(str);
    wchar_t *string = reinterpret_cast<wchar_t*>(ptr.ToPointer());
    return string;
    };

    private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
    /*wchar_t**/ content=TEXT("abc");
    //memcpy((void*)content,content2,4);
    wchar_t* cont2=TEXT("abc");
    int i=0;
    this->textBox1->Text=Pwchar_tToString(cont2);
    }

    private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
    wchar_t* in=StringToPwchar_t(this->textBox1->Text);
    int i=0;
    }
    };


    }

  2. #2
    Join Date
    Mar 2001
    Posts
    2,529

    Re: how to use #pragma data_seq

    #! Use of share memory is really best done with a dll I find:
    #2 Load the DLL into every app that will be using the shared memory.
    #3 Make functions or classes to access that memory.

    Here is an example that shows the ins and outs without getting into too much needless detail:
    in your header:
    Code:
    #ifdef _USRDLL
    	#define LINKDLL __declspec (dllexport) 
    #else
    	#define LINKDLL __declspec (dllimport)
    #endif
    
    LINKDLL void SetValue(int newvalue);
    LINKDLL int GetValue();
    then in your cpp:
    Code:
    #pragma data_seg (".myseg")
     intMyShareInt= 0; 
    #pragma data_seg()
    
    void SetValue(int newvalue)
    {
      MyShareInt=newvalue;
    };
    
    int GetValue()
    {
      return MyShareInt;
    };
    #4 You then must specify the correct sharing attributes in the dll for this new named data section in your .def file or with the linker option
    Code:
    /SECTION:.MYSEC,RWS.
    #5 You will want to add necessary synchronization such as mutex and semaphore to use these functions in multiple processes and threads.

    #6 Please use code tags when posting any amount of code. Highlight your code and click the white code button on the right of the editor.(thanks)

    HTH,
    Last edited by ahoodin; March 20th, 2009 at 02:34 PM.
    ahoodin
    To keep the plot moving, that's why.

  3. #3
    Join Date
    Apr 2003
    Posts
    1,755

    Re: how to use #pragma data_seq

    And share not only the pointer but also the data it is pointing to

    In your code, the "wchar_t* content" is just a pointer that points to a non-shared data. You can make it like "wchar_t content[<yourlength>]" so the other processes can access the actual data correctly.

    Hope it will help you
    Last edited by rxbagain; March 20th, 2009 at 02:51 PM.

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: how to use #pragma data_seq

    #pragma data_seg("Shared")
    volatile wchar_t* content; // !!! this sharing won't work
    #pragma data_seg()
    #pragma comment(linker, "/Section:Shared,RWS")
    The problem with the code is that any unintialized global variable is placed into .bss section, disregarding pragma data_seg specifications. (Please note, data_seg, but not data_seq)

    Code:
    // 11.cpp
    #pragma data_seg("Shared")
    volatile wchar_t* content;
    #pragma data_seg()
    #pragma comment(linker, "/Section:Shared,RWS")
    
    int main()
    {
    	return 0;
    }
    Code:
    cl 11.cpp /link /map:11.map
    Code:
     11
    
     Timestamp is 49c41c78 (Sat Mar 21 01:45:12 2009)
    
     Preferred load address is 00400000
    
     Start         Length     Name                   Class
     0001:00000000 00006254H .text                   CODE
     0002:00000000 000000f4H .idata$5                DATA
     0002:000000f4 00000004H .CRT$XCA                DATA
    . . . 
     0002:0000152c 000000f4H .idata$4                DATA
     0002:00001620 00000452H .idata$6                DATA
     0002:00001a72 00000000H .edata                  DATA
     0003:00000000 00000cc0H .data                   DATA
     0003:00000cc0 00000bbcH .bss                    DATA
    
      Address         Publics by Value              Rva+Base       Lib:Object
    
     0000:00000000       __except_list              00000000     <absolute>
     0000:00000003       ___safe_se_handler_count   00000003     <absolute>
     0000:00000000       ___ImageBase               00400000     <linker-defined>
     0001:00000000       _main                      00401000 f   11.obj
     0001:00000222       _mainCRTStartup            00401222 f   LIBCMT:crt0.obj
    . . .
     0003:00000c50       __lpdays                   0040ac50     LIBCMT:days.obj
     0003:00000c84       __days                     0040ac84     LIBCMT:days.obj
     0003:00000cc0       ?content@@3PC_WC           0040acc0     11.obj
     0003:00000cc4       __aenvptr                  0040acc4     LIBCMT:crt0.obj
     0003:00000cc8       __wenvptr                  0040acc8     LIBCMT:crt0.obj
     0003:00000ccc       ___error_mode              0040accc     LIBCMT:crt0.obj
     0003:00000cd0       __umaskval                 0040acd0     LIBCMT:crt0dat.obj
     0003:00000cd4       __osplatform               0040acd4     LIBCMT:crt0dat.obj
    . . .
    Last edited by Igor Vartanov; March 20th, 2009 at 05:59 PM.
    Best regards,
    Igor

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

Featured