CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2005
    Posts
    1,030

    How to share a resource project among multiple projects?

    I want to share a resource project among mulitple projects. I attached a sample project trying to access a shared resource project. But I got linker errors. Any guru here tells me how to fix the errors? Thanks.
    Attached Files Attached Files

  2. #2
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: How to share a resource project among multiple projects?

    Try to create a DLL project using VS Wizard. You should see something like that:
    Code:
    // The following ifdef block is the standard way of creating macros which make exporting 
    // from a DLL simpler. All files within this DLL are compiled with the DLLTEST_EXPORTS
    // symbol defined on the command line. This symbol should not be defined on any project
    // that uses this DLL. This way any other project whose source files include this file see 
    // DLLTEST_API functions as being imported from a DLL, whereas this DLL sees symbols
    // defined with this macro as being exported.
    #ifdef DLLTEST_EXPORTS
    #define DLLTEST_API __declspec(dllexport)
    #else
    #define DLLTEST_API __declspec(dllimport)
    #endif
    
    // This class is exported from the DllTest.dll
    class DLLTEST_API CDllTest {
    In your example, the TestSharedResourceDlg.cpp includes:
    Code:
    #include "..\SharedResource\SharedDlg.h"
    that has:
    Code:
    class __declspec(dllexport) CSharedDlg : public CDialog
    but SHOULD have __declspec(dllimport)
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  3. #3
    Join Date
    Jul 2005
    Posts
    1,030

    Re: How to share a resource project among multiple projects?

    Quote Originally Posted by VladimirF View Post
    Try to create a DLL project using VS Wizard. You should see something like that:
    Code:
    // The following ifdef block is the standard way of creating macros which make exporting 
    // from a DLL simpler. All files within this DLL are compiled with the DLLTEST_EXPORTS
    // symbol defined on the command line. This symbol should not be defined on any project
    // that uses this DLL. This way any other project whose source files include this file see 
    // DLLTEST_API functions as being imported from a DLL, whereas this DLL sees symbols
    // defined with this macro as being exported.
    #ifdef DLLTEST_EXPORTS
    #define DLLTEST_API __declspec(dllexport)
    #else
    #define DLLTEST_API __declspec(dllimport)
    #endif
    
    // This class is exported from the DllTest.dll
    class DLLTEST_API CDllTest {
    In your example, the TestSharedResourceDlg.cpp includes:
    Code:
    #include "..\SharedResource\SharedDlg.h"
    that has:
    Code:
    class __declspec(dllexport) CSharedDlg : public CDialog
    but SHOULD have __declspec(dllimport)
    Now after I fixed this issue with dllexport/dllimport, I still got two linker errors. I attached the project for you to review to see if there is anything wrong? Thank you very much!
    Attached Files Attached Files

  4. #4
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: How to share a resource project among multiple projects?

    Is this your first DLL project? Have you ever used other DLLs in your projects?
    You have to LINK to the lib that implements these functions.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  5. #5
    Join Date
    Jul 2005
    Posts
    1,030

    Re: How to share a resource project among multiple projects?

    Quote Originally Posted by LarryChen View Post
    Now after I fixed this issue with dllexport/dllimport, I still got two linker errors. I attached the project for you to review to see if there is anything wrong? Thank you very much!
    After I added a reference SharedResource to TestSharedResource from common properties of TestSharedResource, then linker errors are fixed. I don't know why I have to do that under VC2010?
    Right now the projects compile and link fine. But when the project TestSharedResource tried to use resource Dialog from the project SharedResource, the resource dialog can't be loaded. I attached the projects for you to review. Thank you very much!
    Attached Files Attached Files

  6. #6
    Join Date
    Jul 2005
    Posts
    1,030

    Re: How to share a resource project among multiple projects?

    Quote Originally Posted by VladimirF View Post
    Is this your first DLL project? Have you ever used other DLLs in your projects?
    You have to LINK to the lib that implements these functions.
    Before I always use Linker/Advanced/Import Library to link to the lib that implements these functions, but it seems not working under VC2010. I have to add a reference SharedResource to TestSharedResource from common properties of TestSharedResource, then I am able to link to the lib. I already attached an updated version of my project. At this point, I still couldn't load the resource from SharedResource in TestSharedResource. If you have a chance to review the projects, could you figure out why? Thanks.

  7. #7
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: How to share a resource project among multiple projects?

    Quote Originally Posted by LarryChen View Post
    Right now the projects compile and link fine. But when the project TestSharedResource tried to use resource Dialog from the project SharedResource, the resource dialog can't be loaded.
    The MFC's DoModal() looks for the dialog resource in the instance returned by AfxGetResourceHandle(), so you need to temporary replace it with your DLL's instance:
    Code:
    HMODULE hDll = ::LoadLibrary(L"SharedResource.dll");
    HINSTANCE hInst = AfxGetResourceHandle();
    AfxSetResourceHandle(hDll);
    CSharedDlg dlg(this);
    dlg.DoModal();
    AfxSetResourceHandle(hInst);
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  8. #8
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: How to share a resource project among multiple projects?

    Quote Originally Posted by LarryChen View Post
    Before I always use Linker/Advanced/Import Library to link to the lib that implements these functions, but it seems not working under VC2010.
    This "Linker/Advanced/Import Library" produces /IMPLIB command, NOT the input library:
    http://msdn.microsoft.com/en-us/libr...(v=vs.80).aspx
    You need to specify Linker/Input/Additional Dependency
    Quote Originally Posted by LarryChen View Post
    I have to add a reference SharedResource to TestSharedResource from common properties of TestSharedResource, then I am able to link to the lib.
    I don't think you "have" to.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

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