locked
dllimport wcf vs2013 RRS feed

  • Question

  • Hi,

    I created a library.dll, VEC2SQLDLL.DLL in vs2010 on a windows 7 machine.  Along with about 30 applications that use it, I copied it to a Windows 8.1 platform.  Part of this software package is a WCF WSDL service DLL in c# and all that works just fine.

    On Win 8.1, I use Visual Studio 2013.  Here, I am trying to build a new WCF RESTful service DLL in c# that calls to some of the legacy DLLs written in c++.

    I have the following code, copied and only slightly modified  from the vs2010 suite of software:

     public class VCIWCFRestfulLabelTable : IVCIWCFRestfulLabelTable
       {
       
          [DllImport("VEC2SQLDLL.dll", SetLastError = true)]
          extern static String VCISQLGetConnectString(String wcServer);
          
           // GET - the only function this has
           public List<VCI_Name_Entry> svcrGetVCILabelTable()
           {
                List <VCI_Name_Entry> ne = new List<VCI_Name_Entry>();
                SqlConnection sqlcon;
                String sqlconstring = VCISQLGetConnectString(null); // optional pass server name
    

    vec2sqldll.dll is written in c++;

    The imported dll is in the environment path.  The applications from the vs2010 build have no problems loading and using  it.

    However, in vs2013 on the win8.1 platform, the above code snippet runs up to the last line where I get the error:

    "Unable to load DLL 'VEC2SQLDLL.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)"}

    If I place the vec2sqldll.dll in the bin directory that my new WCF DLL is being debugged from, then I get a very lengthy error before debugging begins to execute, long before the above exception break point.

    brief version:

    Could not load file or assembly 'VEC2SqlDLL.DLL' or one of its dependencies. The specified module could not be found. 
      Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
    
     Exception Details: System.IO.FileNotFoundException: Could not load file or assembly 'VEC2SqlDLL.DLL' or one of its dependencies. The specified module could not be found.
    
    Source Error: 
    
    
     An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  
    
    Stack Trace: 
    

    So from that I presume that when the vec2sqldll.dll is found in the path where it is supposed to be, found through the environment PATH; at least the DLL gets loaded.  When copied to the VS2013 output directory, vec2sqldll.dll does not even get loaded.

    So when loaded from it's path location, the new code halts when trying to resolve the entrypoint:

    String sqlconstring = VCISQLGetConnectString(null);

    A call that works just fine in the code built in vs2010.

    Any words of wisdom why the entrypoint is not being found?

    Boyd


    BDM

    Wednesday, February 18, 2015 8:15 PM

Answers

  • Hello Boyd,

    According to your description, I made a test that call a C++ library from the WCF project as:

    public class Service1 : IService1
    
        {
    
            [DllImport(@"MathFuncsDll.dll", EntryPoint = "TestFunc", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
    
            static extern void TestFunc();
    
            public string GetData(int value)
    
            {
    
                TestFunc();
    
                return "Hello";
    
            }
    
        }
    

    The C++ library I used does nothing, I just define a function named TestFunc, and place it in the bin folder of the service. It could work as except, so it should be that dllimport still could work even with WCF project even with VS2013 and windows 8.X.

    My tested function is an empty, it does not have any other custom dependencies, however, from your provided exception, it seems that your C++ library would has some dependencies, I suggest that you could create a as far as possible simple C++ assembly and import it in your wcf project to see if it works, if it works, I think this issue might be caused by these dependencies, for this, you could check this thread which discusses this same issue and provides a workaround:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/d5a92d8e-1c11-44af-b635-4f89471a3351/web-service-dllimport-unable-to-load-dll

    Since as you mentions, the library is built with windows 7 and VS 2010, and you have moved your application to a new one, not sure if there would be incompatible features, i aslo suggesth that you could build the library with new environment to check if it works.

    Or it might be just the CLR could not find the dll path correctly because of some unknow features, for this, you could have a try to use the SetDllDirectory function to provide the dll path at runtime:

    https://msdn.microsoft.com/en-us/library/ms686203(VS.85).aspx

    Here is a link which describes how to use it and a detail explain for how the CLR searches for the DLL:

    http://stackoverflow.com/a/8861895

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Boyd1 Thursday, February 19, 2015 4:55 PM
    Thursday, February 19, 2015 3:09 AM
    Moderator

All replies

  • Hello Boyd,

    According to your description, I made a test that call a C++ library from the WCF project as:

    public class Service1 : IService1
    
        {
    
            [DllImport(@"MathFuncsDll.dll", EntryPoint = "TestFunc", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
    
            static extern void TestFunc();
    
            public string GetData(int value)
    
            {
    
                TestFunc();
    
                return "Hello";
    
            }
    
        }
    

    The C++ library I used does nothing, I just define a function named TestFunc, and place it in the bin folder of the service. It could work as except, so it should be that dllimport still could work even with WCF project even with VS2013 and windows 8.X.

    My tested function is an empty, it does not have any other custom dependencies, however, from your provided exception, it seems that your C++ library would has some dependencies, I suggest that you could create a as far as possible simple C++ assembly and import it in your wcf project to see if it works, if it works, I think this issue might be caused by these dependencies, for this, you could check this thread which discusses this same issue and provides a workaround:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/d5a92d8e-1c11-44af-b635-4f89471a3351/web-service-dllimport-unable-to-load-dll

    Since as you mentions, the library is built with windows 7 and VS 2010, and you have moved your application to a new one, not sure if there would be incompatible features, i aslo suggesth that you could build the library with new environment to check if it works.

    Or it might be just the CLR could not find the dll path correctly because of some unknow features, for this, you could have a try to use the SetDllDirectory function to provide the dll path at runtime:

    https://msdn.microsoft.com/en-us/library/ms686203(VS.85).aspx

    Here is a link which describes how to use it and a detail explain for how the CLR searches for the DLL:

    http://stackoverflow.com/a/8861895

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Boyd1 Thursday, February 19, 2015 4:55 PM
    Thursday, February 19, 2015 3:09 AM
    Moderator
  • Thanks Fred,

    Well if nothing else, your help has shown me that everything is the way it should be.  Good news/ bad news.

    Out of desperation I changed the security settings on the directory that contains my suite of applications and dlls so that EVERYONE has FULL CONTROL.

    Now the service works just fine.

    So I have to determine what the proper security setting should be, but that should be a relatively minor task.

    Thanks for the debugging information.

    Boyd


    BDM

    Thursday, February 19, 2015 4:55 PM