managed C# to unmanaged DLL void*

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

    managed C# to unmanaged DLL void*

    Hi,

    I have a C function in a DLL that takes a void* that is passing back some
    other type, what is the general way to do this with object rather than
    declaring the actual type that its returning.


    ATM I am using the function that has the following signiture


    long someFn(long l, void* data);

    I am calling it in C# as follows...

    [DllImport("some Dll.dll", EntryPoint="som eFn"]
    public static extern int someFn(int l, ref double);

    double d = 0.0;

    int l = someFn(10, ref d);

    Is there a way I can call it with the following type reference pased in?

    object obj;

    someFn(10, ref obj);



  • Willy Denoyette [MVP]

    #2
    Re: managed C# to unmanaged DLL void*

    What makes you think the C function could return a pointer to an .NET object type?

    Willy.


    "Mr.Tickle" <MrTickle@mrmen .com> wrote in message news:e4RGWWPgDH A.696@TK2MSFTNG P09.phx.gbl...[color=blue]
    > Hi,
    >
    > I have a C function in a DLL that takes a void* that is passing back some
    > other type, what is the general way to do this with object rather than
    > declaring the actual type that its returning.
    >
    >
    > ATM I am using the function that has the following signiture
    >
    >
    > long someFn(long l, void* data);
    >
    > I am calling it in C# as follows...
    >
    > [DllImport("some Dll.dll", EntryPoint="som eFn"]
    > public static extern int someFn(int l, ref double);
    >
    > double d = 0.0;
    >
    > int l = someFn(10, ref d);
    >
    > Is there a way I can call it with the following type reference pased in?
    >
    > object obj;
    >
    > someFn(10, ref obj);
    >
    >
    >[/color]


    Comment

    • Mr.Tickle

      #3
      Re: managed C# to unmanaged DLL void*

      Just wanted a generic way of coding a void* instead of having to DllImport
      them to every type I need.


      "Willy Denoyette [MVP]" <willy.denoyett e@pandora.be> wrote in message
      news:#SXFnaPgDH A.128@tk2msftng p13.phx.gbl...[color=blue]
      > What makes you think the C function could return a pointer to an .NET[/color]
      object type?[color=blue]
      >
      > Willy.
      >
      >
      > "Mr.Tickle" <MrTickle@mrmen .com> wrote in message[/color]
      news:e4RGWWPgDH A.696@TK2MSFTNG P09.phx.gbl...[color=blue][color=green]
      > > Hi,
      > >
      > > I have a C function in a DLL that takes a void* that is passing back[/color][/color]
      some[color=blue][color=green]
      > > other type, what is the general way to do this with object rather than
      > > declaring the actual type that its returning.
      > >
      > >
      > > ATM I am using the function that has the following signiture
      > >
      > >
      > > long someFn(long l, void* data);
      > >
      > > I am calling it in C# as follows...
      > >
      > > [DllImport("some Dll.dll", EntryPoint="som eFn"]
      > > public static extern int someFn(int l, ref double);
      > >
      > > double d = 0.0;
      > >
      > > int l = someFn(10, ref d);
      > >
      > > Is there a way I can call it with the following type reference pased in?
      > >
      > > object obj;
      > >
      > > someFn(10, ref obj);
      > >
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Mr.Tickle

        #4
        Re: managed C# to unmanaged DLL void*

        Well I have it as...

        [DllImport("some Dll.dll", EntryPoint="som eFn")]
        public static extern int someFn(int l, [MarshalAs(Unman agedType.AsAny)]ref
        object);

        [DllImport("some Dll.dll", EntryPoint="som eFn")]
        public static extern int someFn(int l, ref double);


        But I dont want to have X number of DllImport lines overloaded for every
        type I have to pass and get back. There has got to be an easier less
        verbose way surely.



        "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
        news:uzY6lBSgDH A.2364@TK2MSFTN GP09.phx.gbl...[color=blue]
        > Just wanted a generic way of coding a void* instead of having to DllImport
        > them to every type I need.
        >
        >
        > "Willy Denoyette [MVP]" <willy.denoyett e@pandora.be> wrote in message
        > news:#SXFnaPgDH A.128@tk2msftng p13.phx.gbl...[color=green]
        > > What makes you think the C function could return a pointer to an .NET[/color]
        > object type?[color=green]
        > >
        > > Willy.
        > >
        > >
        > > "Mr.Tickle" <MrTickle@mrmen .com> wrote in message[/color]
        > news:e4RGWWPgDH A.696@TK2MSFTNG P09.phx.gbl...[color=green][color=darkred]
        > > > Hi,
        > > >
        > > > I have a C function in a DLL that takes a void* that is passing[/color][/color][/color]
        back[color=blue]
        > some[color=green][color=darkred]
        > > > other type, what is the general way to do this with object rather than
        > > > declaring the actual type that its returning.
        > > >
        > > >
        > > > ATM I am using the function that has the following signiture
        > > >
        > > >
        > > > long someFn(long l, void* data);
        > > >
        > > > I am calling it in C# as follows...
        > > >
        > > > [DllImport("some Dll.dll", EntryPoint="som eFn"]
        > > > public static extern int someFn(int l, ref double);
        > > >
        > > > double d = 0.0;
        > > >
        > > > int l = someFn(10, ref d);
        > > >
        > > > Is there a way I can call it with the following type reference pased[/color][/color][/color]
        in?[color=blue][color=green][color=darkred]
        > > >
        > > > object obj;
        > > >
        > > > someFn(10, ref obj);
        > > >
        > > >
        > > >[/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Mattias Sjögren

          #5
          Re: managed C# to unmanaged DLL void*

          [color=blue]
          >But I dont want to have X number of DllImport lines overloaded for every
          >type I have to pass and get back.[/color]

          How many overloads are we talking about?

          [color=blue]
          >There has got to be an easier less verbose way surely.[/color]

          If you don't care about type safety, you could declare the method as
          unsafe and actually type the parameter as void*.



          Mattias

          --
          Mattias Sjögren [MVP] mattias @ mvps.org

          Please reply only to the newsgroup.

          Comment

          • Mr.Tickle

            #6
            Re: managed C# to unmanaged DLL void*

            I dont know how many yet but quite a few as I am wrapping a huge amount of
            DLL functions.

            "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
            news:uR26u8TgDH A.3700@TK2MSFTN GP11.phx.gbl...[color=blue]
            >[color=green]
            > >But I dont want to have X number of DllImport lines overloaded for every
            > >type I have to pass and get back.[/color]
            >
            > How many overloads are we talking about?
            >
            >[color=green]
            > >There has got to be an easier less verbose way surely.[/color]
            >
            > If you don't care about type safety, you could declare the method as
            > unsafe and actually type the parameter as void*.
            >
            >
            >
            > Mattias
            >
            > --
            > Mattias Sjögren [MVP] mattias @ mvps.org
            > http://www.msjogren.net/dotnet/
            > Please reply only to the newsgroup.[/color]


            Comment

            • Mr.Tickle

              #7
              Re: managed C# to unmanaged DLL void*

              How come [MarshalAs(Unman agedType.AsAny)] on the ref object parameter cant
              resolve the type at runtime for every known type where possible?

              I have one marshalled .AsAny but what purpose will that serve if I have to
              overload them with the types as it seems I have to do.


              "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
              news:uR26u8TgDH A.3700@TK2MSFTN GP11.phx.gbl...[color=blue]
              >[color=green]
              > >But I dont want to have X number of DllImport lines overloaded for every
              > >type I have to pass and get back.[/color]
              >
              > How many overloads are we talking about?
              >
              >[color=green]
              > >There has got to be an easier less verbose way surely.[/color]
              >
              > If you don't care about type safety, you could declare the method as
              > unsafe and actually type the parameter as void*.
              >
              >
              >
              > Mattias
              >
              > --
              > Mattias Sjögren [MVP] mattias @ mvps.org
              > http://www.msjogren.net/dotnet/
              > Please reply only to the newsgroup.[/color]


              Comment

              Working...