[DLLImport] Attribute where is default dll location

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lenn

    [DLLImport] Attribute where is default dll location

    Hi,

    I have a DLL with unmanaged function written in C. I need to call from
    managed C# code, where should DLL have to placed, bin folder system32? And
    can this location be changed. I haven't found anything useful in this regard
    on MSDN documentation.
    Thank you

  • Robbe Morris [C# MVP]

    #2
    Re: [DLLImport] Attribute where is default dll location

    If it is registered in the registry, you should be able
    to just add a COM reference to it in your project.

    --
    Robbe Morris - 2004/2005 Microsoft MVP C#






    "Lenn" <Lenn@discussio ns.microsoft.co m> wrote in message
    news:4D380FFF-C44A-47F1-9F0F-AE0362278A60@mi crosoft.com...[color=blue]
    > Hi,
    >
    > I have a DLL with unmanaged function written in C. I need to call from
    > managed C# code, where should DLL have to placed, bin folder system32? And
    > can this location be changed. I haven't found anything useful in this
    > regard
    > on MSDN documentation.
    > Thank you
    >[/color]


    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: [DLLImport] Attribute where is default dll location

      Robbe,

      Two things here.

      First, the poster was asking for the search path for the DllImport
      attribute, which is used with the P/Invoke layer, not COM interop. In this
      case, the P/Invoke layer uses LoadLibrary to load the DLL, and will follow
      those rules for locating the DLL.

      This means that you can also call LoadLibrary yourself if you need to
      load the DLL outside of a location in the search path. This way, the DLL is
      loaded in the process, and the P/Invoke call will succeed.

      Also, in reference to COM, in XP, you can actually have
      registration-free COM, by placing the details of the COM server in your
      manifest file. Calls to CoCreateInstanc e/CoCreateInstanc eEx will then look
      through the loaded details from the manifest file first, and then in the
      registry.

      Hope this helps.

      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m

      "Robbe Morris [C# MVP]" <info@eggheadca fe.com> wrote in message
      news:uQW3QLVDGH A.272@TK2MSFTNG P10.phx.gbl...[color=blue]
      > If it is registered in the registry, you should be able
      > to just add a COM reference to it in your project.
      >
      > --
      > Robbe Morris - 2004/2005 Microsoft MVP C#
      > http://www.eggheadcafe.com/forums/merit.asp
      >
      >
      >
      >
      >
      > "Lenn" <Lenn@discussio ns.microsoft.co m> wrote in message
      > news:4D380FFF-C44A-47F1-9F0F-AE0362278A60@mi crosoft.com...[color=green]
      >> Hi,
      >>
      >> I have a DLL with unmanaged function written in C. I need to call from
      >> managed C# code, where should DLL have to placed, bin folder system32?
      >> And
      >> can this location be changed. I haven't found anything useful in this
      >> regard
      >> on MSDN documentation.
      >> Thank you
      >>[/color]
      >
      >[/color]


      Comment

      • Lenn

        #4
        Re: [DLLImport] Attribute where is default dll location

        Thanks,

        Yes, it's not COM object. It's one DLL file library of C function(s).
        [color=blue]
        > First, the poster was asking for the search path for the DllImport
        > attribute, which is used with the P/Invoke layer, not COM interop. In this
        > case, the P/Invoke layer uses LoadLibrary to load the DLL, and will follow
        > those rules for locating the DLL.[/color]

        What are the rules?
        [color=blue]
        > This means that you can also call LoadLibrary yourself if you need to
        > load the DLL outside of a location in the search path. This way, the DLL is
        > loaded in the process, and the P/Invoke call will succeed.[/color]

        Do you have any code samples, links?

        Thank you

        "Nicholas Paldino [.NET/C# MVP]" wrote:
        [color=blue]
        > Robbe,
        >
        > Two things here.
        >
        > First, the poster was asking for the search path for the DllImport
        > attribute, which is used with the P/Invoke layer, not COM interop. In this
        > case, the P/Invoke layer uses LoadLibrary to load the DLL, and will follow
        > those rules for locating the DLL.
        >
        > This means that you can also call LoadLibrary yourself if you need to
        > load the DLL outside of a location in the search path. This way, the DLL is
        > loaded in the process, and the P/Invoke call will succeed.
        >
        > Also, in reference to COM, in XP, you can actually have
        > registration-free COM, by placing the details of the COM server in your
        > manifest file. Calls to CoCreateInstanc e/CoCreateInstanc eEx will then look
        > through the loaded details from the manifest file first, and then in the
        > registry.
        >
        > Hope this helps.
        >
        > --
        > - Nicholas Paldino [.NET/C# MVP]
        > - mvp@spam.guard. caspershouse.co m
        >
        > "Robbe Morris [C# MVP]" <info@eggheadca fe.com> wrote in message
        > news:uQW3QLVDGH A.272@TK2MSFTNG P10.phx.gbl...[color=green]
        > > If it is registered in the registry, you should be able
        > > to just add a COM reference to it in your project.
        > >
        > > --
        > > Robbe Morris - 2004/2005 Microsoft MVP C#
        > > http://www.eggheadcafe.com/forums/merit.asp
        > >
        > >
        > >
        > >
        > >
        > > "Lenn" <Lenn@discussio ns.microsoft.co m> wrote in message
        > > news:4D380FFF-C44A-47F1-9F0F-AE0362278A60@mi crosoft.com...[color=darkred]
        > >> Hi,
        > >>
        > >> I have a DLL with unmanaged function written in C. I need to call from
        > >> managed C# code, where should DLL have to placed, bin folder system32?
        > >> And
        > >> can this location be changed. I haven't found anything useful in this
        > >> regard
        > >> on MSDN documentation.
        > >> Thank you
        > >>[/color]
        > >
        > >[/color]
        >
        >
        >[/color]

        Comment

        • Willy Denoyette [MVP]

          #5
          Re: [DLLImport] Attribute where is default dll location

          The search order used by the OS is:
          1.. The directory where the executable module for the current process is
          located.
          2.. The current directory.The Windows system directory.

          3.. The "Windows System" directory. (e.g c:\Windows\Syst em32)

          4.. The "Windows" directory. (e.g c:\Windows)

          5.. The directories listed in the PATH environment variable.

          Willy.

          "Lenn" <Lenn@discussio ns.microsoft.co m> wrote in message
          news:9963A631-A661-4D55-B39F-BE2F32826766@mi crosoft.com...[color=blue]
          > Thanks,
          >
          > Yes, it's not COM object. It's one DLL file library of C function(s).
          >[color=green]
          >> First, the poster was asking for the search path for the DllImport
          >> attribute, which is used with the P/Invoke layer, not COM interop. In
          >> this
          >> case, the P/Invoke layer uses LoadLibrary to load the DLL, and will
          >> follow
          >> those rules for locating the DLL.[/color]
          >
          > What are the rules?
          >[/color]

          The search order used by the OS is:
          1.. The directory where the executable module for the current process is
          located.
          2.. The current directory.The Windows system directory.

          3.. The "Windows System" directory. (e.g c:\Windows\Syst em32)

          4.. The "Windows" directory. (e.g c:\Windows)

          5.. The directories listed in the PATH environment variable.

          Check MSDN for details on LoadLibrary and LoadLibraryEx.
          [color=blue][color=green]
          >> This means that you can also call LoadLibrary yourself if you need to
          >> load the DLL outside of a location in the search path. This way, the DLL
          >> is
          >> loaded in the process, and the P/Invoke call will succeed.[/color]
          >
          > Do you have any code samples, links?[/color]

          Mind to google for LoadLibrary and CSharp?

          Willy.



          Comment

          • Lenn

            #6
            Re: [DLLImport] Attribute where is default dll location

            That's great. Thanks

            "Willy Denoyette [MVP]" wrote:
            [color=blue]
            > The search order used by the OS is:
            > 1.. The directory where the executable module for the current process is
            > located.
            > 2.. The current directory.The Windows system directory.
            >
            > 3.. The "Windows System" directory. (e.g c:\Windows\Syst em32)
            >
            > 4.. The "Windows" directory. (e.g c:\Windows)
            >
            > 5.. The directories listed in the PATH environment variable.
            >
            > Willy.
            >
            > "Lenn" <Lenn@discussio ns.microsoft.co m> wrote in message
            > news:9963A631-A661-4D55-B39F-BE2F32826766@mi crosoft.com...[color=green]
            > > Thanks,
            > >
            > > Yes, it's not COM object. It's one DLL file library of C function(s).
            > >[color=darkred]
            > >> First, the poster was asking for the search path for the DllImport
            > >> attribute, which is used with the P/Invoke layer, not COM interop. In
            > >> this
            > >> case, the P/Invoke layer uses LoadLibrary to load the DLL, and will
            > >> follow
            > >> those rules for locating the DLL.[/color]
            > >
            > > What are the rules?
            > >[/color]
            >
            > The search order used by the OS is:
            > 1.. The directory where the executable module for the current process is
            > located.
            > 2.. The current directory.The Windows system directory.
            >
            > 3.. The "Windows System" directory. (e.g c:\Windows\Syst em32)
            >
            > 4.. The "Windows" directory. (e.g c:\Windows)
            >
            > 5.. The directories listed in the PATH environment variable.
            >
            > Check MSDN for details on LoadLibrary and LoadLibraryEx.
            >[color=green][color=darkred]
            > >> This means that you can also call LoadLibrary yourself if you need to
            > >> load the DLL outside of a location in the search path. This way, the DLL
            > >> is
            > >> loaded in the process, and the P/Invoke call will succeed.[/color]
            > >
            > > Do you have any code samples, links?[/color]
            >
            > Mind to google for LoadLibrary and CSharp?
            >
            > Willy.
            >
            >
            >
            >[/color]

            Comment

            • Robbe Morris [C# MVP]

              #7
              Yep, my bad

              I read C and "thought C++" and assumed it was COM.

              You is correct

              --
              Robbe Morris - 2004/2005 Microsoft MVP C#






              "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
              message news:OWUCHUVDGH A.2300@TK2MSFTN GP15.phx.gbl...[color=blue]
              > Robbe,
              >
              > Two things here.
              >
              > First, the poster was asking for the search path for the DllImport
              > attribute, which is used with the P/Invoke layer, not COM interop. In
              > this case, the P/Invoke layer uses LoadLibrary to load the DLL, and will
              > follow those rules for locating the DLL.
              >
              > This means that you can also call LoadLibrary yourself if you need to
              > load the DLL outside of a location in the search path. This way, the DLL
              > is loaded in the process, and the P/Invoke call will succeed.
              >
              > Also, in reference to COM, in XP, you can actually have
              > registration-free COM, by placing the details of the COM server in your
              > manifest file. Calls to CoCreateInstanc e/CoCreateInstanc eEx will then
              > look through the loaded details from the manifest file first, and then in
              > the registry.
              >
              > Hope this helps.
              >
              > --
              > - Nicholas Paldino [.NET/C# MVP]
              > - mvp@spam.guard. caspershouse.co m
              >
              > "Robbe Morris [C# MVP]" <info@eggheadca fe.com> wrote in message
              > news:uQW3QLVDGH A.272@TK2MSFTNG P10.phx.gbl...[color=green]
              >> If it is registered in the registry, you should be able
              >> to just add a COM reference to it in your project.
              >>
              >> --
              >> Robbe Morris - 2004/2005 Microsoft MVP C#
              >> http://www.eggheadcafe.com/forums/merit.asp
              >>
              >>
              >>
              >>
              >>
              >> "Lenn" <Lenn@discussio ns.microsoft.co m> wrote in message
              >> news:4D380FFF-C44A-47F1-9F0F-AE0362278A60@mi crosoft.com...[color=darkred]
              >>> Hi,
              >>>
              >>> I have a DLL with unmanaged function written in C. I need to call from
              >>> managed C# code, where should DLL have to placed, bin folder system32?
              >>> And
              >>> can this location be changed. I haven't found anything useful in this
              >>> regard
              >>> on MSDN documentation.
              >>> Thank you
              >>>[/color]
              >>
              >>[/color]
              >
              >[/color]


              Comment

              Working...