C#: DLL Injection

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

    C#: DLL Injection

    Hello,

    I'm curious if anyone could describe the settings needed for a C#
    project to inject a C dll (setup a windows hook) into a running process.

    Thanks,
    Ryan
  • Lebesgue

    #2
    Re: DLL Injection

    It is impossible to do this in pure C#. You can write a C dll which injects
    code and use it in C# via P/Invoke using DllImport attribute to import and
    use native functions from your C dll.

    "Ryan Ross" <ryanr@nni.co m> wrote in message
    news:2cCdnZTSLt OAXvTfRVn-hg@nni.com...[color=blue]
    > Hello,
    >
    > I'm curious if anyone could describe the settings needed for a C#
    > project to inject a C dll (setup a windows hook) into a running process.
    >
    > Thanks,
    > Ryan[/color]


    Comment

    • Ryan Ross

      #3
      Re: DLL Injection

      Yes. The problem I'm faced with is that the C dll does not appear to be
      working. More precisely, the dll injects just fine in the alpha code,
      but appears not to work in the beta code. Here's some snippets:

      The DLL injection code (same in both the alpha and the beta):


      public class PaintHook
      {
      [DllImport ("PaintHook.dll ", EntryPoint="#1" )]
      public static extern IntPtr InstallHook();

      }

      This code works fine in the alpha, but does not work in the beta.
      Invocation of the dll is PaintHook.Insta llHook(). The dll is activated
      by sending a custom message via SendMessage().

      For the life of me, I cannot figure out why it's not working. The dll
      copies a window into a bitmap (which exists in shared memory, which the
      C# component accesses), something akin to the PrintWindow() function.

      As a test, I changed the dll to close a window when activated. Alpha
      code closes the window, beta does not. Very simple code (InstallHook,
      SendMessage methods), should work in both. No dice.

      Any theories as to why the dll works only in the one? Am I missing some
      security or project settings that are the cause?

      Thanks,
      Ryan

      Lebesgue wrote:[color=blue]
      > It is impossible to do this in pure C#. You can write a C dll which injects
      > code and use it in C# via P/Invoke using DllImport attribute to import and
      > use native functions from your C dll.
      >
      > "Ryan Ross" <ryanr@nni.co m> wrote in message
      > news:2cCdnZTSLt OAXvTfRVn-hg@nni.com...
      >[color=green]
      >>Hello,
      >>
      >>I'm curious if anyone could describe the settings needed for a C#
      >>project to inject a C dll (setup a windows hook) into a running process.
      >>
      >>Thanks,
      >>Ryan[/color]
      >
      >
      >[/color]

      Comment

      Working...