CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2004
    Posts
    123

    Loading Global data???

    How do i load global data in C#???

    Also,how do i start a component added as reference to my project,without calling it...i.e assume that you have an exe and a dll...on executing the exe the dll also should start executing(some how i need to specify a method in the dll as the entry point of it),but there should not be any kind of calls going from the exe to the dll...Is there any ways of doing it???

  2. #2
    Join Date
    Dec 2005
    Location
    Waterloo ON
    Posts
    545

    Re: Loading Global data???

    You want the dll running right after the exe runs, but don't call the dll from exe. ???

    You can convert the dll to windows service, and then put a timer in it to detect if the exe is running, if yes run specified code.

    btw, where is the global data from?
    The difficulty is that you have no idea how difficult it is.

    .Net 3.5/VS 2008

  3. #3
    Join Date
    Apr 2004
    Posts
    123

    Re: Loading Global data???

    Assume that i have a component which basically loads global data(data which is going to be used across entire application) and i add a reference of this project to my main exe and that is all what i do.There should not be any call from the exe to the dll,the dll inturn should detect that application has booted up and should start retrieving the data.

    I know the question is kind of weird and it's highly impossible in normal terms..but still is possible.

    Does some body have experience with C++,which does something on similar grounds???

    Static and static constructors i guess is not a solution to this problem,cos both requires a call to be originated from the exe.

  4. #4
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: Loading Global data???

    If your executable does not make any call to the the DLL how will it get the data that the DLL contains? Ultimately your executable will have to get the data through some method or property on a class in your DLL. In any case you have a few options.
    Static and static constructors i guess is not a solution to this problem,cos both requires a call to be originated from the exe.
    I'm not aware of this limitation. I'm currently using static constructors in DLLs without any problem. Perhaps there's something I missed from your description.

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Loading Global data???

    Quote Originally Posted by mmx_nexus
    Does some body have experience with C++,which does something on similar grounds???
    Are you referring to the DLL_PROCESS_ATTACH functionality in a Win32 dll?

    Code:
    BOOL APIENTRY DllMain( HANDLE hModule, 
                           DWORD  ul_reason_for_call, 
                           LPVOID lpReserved
    					 )
    {
    	switch (ul_reason_for_call)
    	{
    	case DLL_PROCESS_ATTACH:
    	case DLL_THREAD_ATTACH:
    		// Perform some initialization functionality here?
    		break;
    	case DLL_THREAD_DETACH:
    	case DLL_PROCESS_DETACH:
    		break;
    	}
        return TRUE;
    }

  6. #6
    Join Date
    Apr 2004
    Posts
    123

    Re: Loading Global data???

    OK, i see that my post was bit confusing..

    Let me give you an exact scenario of what i want...

    I have a component which sends my GUI clients memory profile to the server.I need this dll to start doing work,without being called from my exe.I do have a reference of the dll,but do not make a call to it.The dll starts on it's own on a seperate thread or something and starts sending information to the server,sniffing the state of the GUI client.Is this possible???

    Thanks,
    Mmx

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Loading Global data???

    The reason I posted the Win32 dll code is bring up the concept of receiving an event with the dll is loaded. For Win32, this was DLL_PROCESS_ATTACH (okay this isn't truly an event, but accomplishes the same thing).

    Now on to the .net dll equivalents which are class library assemblies. Unfortunately, these don't have any OnLoad type events so you are out of luck there.

    But...

    An executable assembly does have a main function so you can perform initialization there.

    Unfortunately you are going to have to explicitly call into the dll to start things off. Using a singleton class would make things easier (such as one called MemoryProfiler):

    Code:
            [STAThread]
            static void Main( )
            {
                MemoryProfiler.Instance.Start( );
    
                Application.EnableVisualStyles( );
                Application.SetCompatibleTextRenderingDefault( false );
                Application.Run( new Form1( ) );
    
                MemoryProfiler.Instance.Stop( );
            }
    If you wanted to ensure that the Stop function was called you could leverage IDisposable and use a using block as follow. Create would call Start and Stop would get implicitly called when the object goes out of scope.

    Code:
            [STAThread]
            static void Main( )
            {
                using( MemoryProfiler mp = MemoryProfiler.Create( ) )
                {
                            Application.EnableVisualStyles( );
                            Application.SetCompatibleTextRenderingDefault( false );
                            Application.Run( new Form1( ) );
                }
            }

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