DllMain not called

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

DllMain not called

Olivia Nelson
I have a simple C program that output something to DbgView on startup:

#include <windows.h>
BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
        OutputDebugStringA("DLL_PROCESS_ATTACH called");
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

I didn't observe anything when the DLL is loaded, what was wrong?

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

Reply | Threaded
Open this post in threaded view
|

Re: DllMain not called

marco atzeri-4
Am 07.02.2020 um 09:46 schrieb Olivia Nelson:

> I have a simple C program that output something to DbgView on startup:
>
> #include <windows.h>
> BOOL APIENTRY DllMain( HMODULE hModule,
>                         DWORD  ul_reason_for_call,
>                         LPVOID lpReserved
>                       )
> {
>      switch (ul_reason_for_call)
>      {
>      case DLL_PROCESS_ATTACH:
>          OutputDebugStringA("DLL_PROCESS_ATTACH called");
>      case DLL_THREAD_ATTACH:
>      case DLL_THREAD_DETACH:
>      case DLL_PROCESS_DETACH:
>          break;
>      }
>      return TRUE;
> }
>
> I didn't observe anything when the DLL is loaded, what was wrong?
>

This seems a pure Windows program, not a Unix like.

How you compiled it and how are you tring to run it ?

Regards
Marco

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

Reply | Threaded
Open this post in threaded view
|

Re: DllMain not called

Olivia Nelson
I compile it like this:
gcc dll.c -shared -fPIC -o dll.dll

I tried to run it with LoadLibraryA

But DllMain is not called.


On Fri, Feb 7, 2020 at 10:49 PM Marco Atzeri <[hidden email]> wrote:

>
> Am 07.02.2020 um 09:46 schrieb Olivia Nelson:
> > I have a simple C program that output something to DbgView on startup:
> >
> > #include <windows.h>
> > BOOL APIENTRY DllMain( HMODULE hModule,
> >                         DWORD  ul_reason_for_call,
> >                         LPVOID lpReserved
> >                       )
> > {
> >      switch (ul_reason_for_call)
> >      {
> >      case DLL_PROCESS_ATTACH:
> >          OutputDebugStringA("DLL_PROCESS_ATTACH called");
> >      case DLL_THREAD_ATTACH:
> >      case DLL_THREAD_DETACH:
> >      case DLL_PROCESS_DETACH:
> >          break;
> >      }
> >      return TRUE;
> > }
> >
> > I didn't observe anything when the DLL is loaded, what was wrong?
> >
>
> This seems a pure Windows program, not a Unix like.
>
> How you compiled it and how are you tring to run it ?
>
> Regards
> Marco
>
> --
> Problem reports:       http://cygwin.com/problems.html
> FAQ:                   http://cygwin.com/faq/
> Documentation:         http://cygwin.com/docs.html
> Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
>

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

Reply | Threaded
Open this post in threaded view
|

Re: DllMain not called

Joost Kraaijeveld
Does

static void __attribute__((constructor))
static void __attribute__((destructor))

work?


See e.g.
https://stackoverflow.com/questions/2053029/how-exactly-does-attribute-constructor-work




--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

Reply | Threaded
Open this post in threaded view
|

Re: DllMain not called

marco atzeri-4
In reply to this post by Olivia Nelson
Am 07.02.2020 um 16:07 schrieb Olivia Nelson:
> I compile it like this:
> gcc dll.c -shared -fPIC -o dll.dll
>
> I tried to run it with LoadLibraryA
>
> But DllMain is not called.
>
>

you are building a Cygwin dll, that requires the Cygwin1.dll
but probably do you not intend it and need a standalone one that
requires only the MS system dll's.

install the Mingw crosscompiler x86_64-w64-mingw32-gcc.exe
that is in the package

$ cygcheck -f /usr/bin/x86_64-w64-mingw32-gcc.exe
mingw64-x86_64-gcc-core-7.4.0-1

and compile as
x86_64-w64-mingw32-gcc.exe -Wall dll.c -shared -fPIC -o dll.dll

Regards
Marco

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

Reply | Threaded
Open this post in threaded view
|

Re: DllMain not called

Joost Kraaijeveld
In reply to this post by Olivia Nelson
Does

static void __attribute__((constructor))
static void __attribute__((destructor))

work?

See also:
https://stackoverflow.com/questions/2053029/how-exactly-does-attribute-constructor-work




--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

Reply | Threaded
Open this post in threaded view
|

Re: DllMain not called in CreateRemoteThread

Olivia Nelson
In reply to this post by Joost Kraaijeveld
Sorry, I made a mistake. I've updated the title ..

To summarize:

1. I can load the DLL compiled with cygwin with LoadLibraryA,
     but not remotely with CreateRemoteThread.
2. The same DLL compiled with msvc works with both LoadLibraryA and
CreateRemoteThread

Answer to Joost:
constructor and destructor are called with LoadLibraryA, but not with
CreateRemoteThread either

Answer to Marco
1. I can't use mingw, it does not provide sys/XXX.h, e,g socket
2. I copied cygwin1.dll to system32, and it works outside cygwin environment

Here's a implementation of the DLL injector in C#:
https://codingvision.net/miscellaneous/c-inject-a-dll-into-a-process-w-createremotethread


On Sat, Feb 8, 2020 at 3:10 AM Joost Kraaijeveld
<[hidden email]> wrote:

>
> Does
>
> static void __attribute__((constructor))
> static void __attribute__((destructor))
>
> work?
>
>
> See e.g.
> https://stackoverflow.com/questions/2053029/how-exactly-does-attribute-constructor-work
>
>
>
>
> --
> Problem reports:       http://cygwin.com/problems.html
> FAQ:                   http://cygwin.com/faq/
> Documentation:         http://cygwin.com/docs.html
> Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
>

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

Reply | Threaded
Open this post in threaded view
|

Re: DllMain not called in CreateRemoteThread

marco atzeri-4
please bottom post on this mailing list

Am 08.02.2020 um 01:12 schrieb Olivia Nelson:

> Sorry, I made a mistake. I've updated the title ..
>
> To summarize:
>
> 1. I can load the DLL compiled with cygwin with LoadLibraryA,
>       but not remotely with CreateRemoteThread.
> 2. The same DLL compiled with msvc works with both LoadLibraryA and
> CreateRemoteThread
>
> Answer to Joost:
> constructor and destructor are called with LoadLibraryA, but not with
> CreateRemoteThread either
>
> Answer to Marco
> 1. I can't use mingw, it does not provide sys/XXX.h, e,g socket
> 2. I copied cygwin1.dll to system32, and it works outside cygwin environment
>
> Here's a implementation of the DLL injector in C#:
> https://codingvision.net/miscellaneous/c-inject-a-dll-into-a-process-w-createremotethread
>

Which of the sys/XXX.h are you looking for ?

$ cygcheck -l mingw64-x86_64-headers | grep "/sys/"
/usr/x86_64-w64-mingw32/sys-root/mingw/include/sec_api/sys/timeb_s.h
/usr/x86_64-w64-mingw32/sys-root/mingw/include/sys/cdefs.h
/usr/x86_64-w64-mingw32/sys-root/mingw/include/sys/fcntl.h
/usr/x86_64-w64-mingw32/sys-root/mingw/include/sys/file.h
/usr/x86_64-w64-mingw32/sys-root/mingw/include/sys/locking.h
/usr/x86_64-w64-mingw32/sys-root/mingw/include/sys/param.h
/usr/x86_64-w64-mingw32/sys-root/mingw/include/sys/stat.h
/usr/x86_64-w64-mingw32/sys-root/mingw/include/sys/time.h
/usr/x86_64-w64-mingw32/sys-root/mingw/include/sys/timeb.h
/usr/x86_64-w64-mingw32/sys-root/mingw/include/sys/types.h
/usr/x86_64-w64-mingw32/sys-root/mingw/include/sys/unistd.h
/usr/x86_64-w64-mingw32/sys-root/mingw/include/sys/utime.h

for what I know the Windows program use winsock2.h and winsock.h
for their own socket interface

$ cygcheck -l mingw64-x86_64-headers | grep "sock"
/usr/x86_64-w64-mingw32/sys-root/mingw/include/hvsocket.h
/usr/x86_64-w64-mingw32/sys-root/mingw/include/mswsock.h
/usr/x86_64-w64-mingw32/sys-root/mingw/include/psdk_inc/_socket_types.h
/usr/x86_64-w64-mingw32/sys-root/mingw/include/winsock.h
/usr/x86_64-w64-mingw32/sys-root/mingw/include/winsock2.h


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

Reply | Threaded
Open this post in threaded view
|

Re: DllMain not called in CreateRemoteThread

Olivia Nelson
I need to use unix socket() interface, mingw does not seem to provide
the following headers:

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/mman.h>
#include <sys/wait.h>


On Sat, Feb 8, 2020 at 1:40 PM Marco Atzeri <[hidden email]> wrote:

>
> please bottom post on this mailing list
>
> Am 08.02.2020 um 01:12 schrieb Olivia Nelson:
> > Sorry, I made a mistake. I've updated the title ..
> >
> > To summarize:
> >
> > 1. I can load the DLL compiled with cygwin with LoadLibraryA,
> >       but not remotely with CreateRemoteThread.
> > 2. The same DLL compiled with msvc works with both LoadLibraryA and
> > CreateRemoteThread
> >
> > Answer to Joost:
> > constructor and destructor are called with LoadLibraryA, but not with
> > CreateRemoteThread either
> >
> > Answer to Marco
> > 1. I can't use mingw, it does not provide sys/XXX.h, e,g socket
> > 2. I copied cygwin1.dll to system32, and it works outside cygwin environment
> >
> > Here's a implementation of the DLL injector in C#:
> > https://codingvision.net/miscellaneous/c-inject-a-dll-into-a-process-w-createremotethread
> >
>
> Which of the sys/XXX.h are you looking for ?
>
> $ cygcheck -l mingw64-x86_64-headers | grep "/sys/"
> /usr/x86_64-w64-mingw32/sys-root/mingw/include/sec_api/sys/timeb_s.h
> /usr/x86_64-w64-mingw32/sys-root/mingw/include/sys/cdefs.h
> /usr/x86_64-w64-mingw32/sys-root/mingw/include/sys/fcntl.h
> /usr/x86_64-w64-mingw32/sys-root/mingw/include/sys/file.h
> /usr/x86_64-w64-mingw32/sys-root/mingw/include/sys/locking.h
> /usr/x86_64-w64-mingw32/sys-root/mingw/include/sys/param.h
> /usr/x86_64-w64-mingw32/sys-root/mingw/include/sys/stat.h
> /usr/x86_64-w64-mingw32/sys-root/mingw/include/sys/time.h
> /usr/x86_64-w64-mingw32/sys-root/mingw/include/sys/timeb.h
> /usr/x86_64-w64-mingw32/sys-root/mingw/include/sys/types.h
> /usr/x86_64-w64-mingw32/sys-root/mingw/include/sys/unistd.h
> /usr/x86_64-w64-mingw32/sys-root/mingw/include/sys/utime.h
>
> for what I know the Windows program use winsock2.h and winsock.h
> for their own socket interface
>
> $ cygcheck -l mingw64-x86_64-headers | grep "sock"
> /usr/x86_64-w64-mingw32/sys-root/mingw/include/hvsocket.h
> /usr/x86_64-w64-mingw32/sys-root/mingw/include/mswsock.h
> /usr/x86_64-w64-mingw32/sys-root/mingw/include/psdk_inc/_socket_types.h
> /usr/x86_64-w64-mingw32/sys-root/mingw/include/winsock.h
> /usr/x86_64-w64-mingw32/sys-root/mingw/include/winsock2.h
>
>
> --
> Problem reports:       http://cygwin.com/problems.html
> FAQ:                   http://cygwin.com/faq/
> Documentation:         http://cygwin.com/docs.html
> Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
>

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

Reply | Threaded
Open this post in threaded view
|

Re: DllMain not called in CreateRemoteThread

Andrey Repin
Greetings, Olivia Nelson!

> I need to use unix socket() interface, mingw does not seem to provide
> the following headers:

There's no UNIX interfaces in native applications.
Choose one or another, not both.

> #include <sys/types.h>
> #include <sys/socket.h>
> #include <sys/mman.h>
> #include <sys/wait.h>


Also, please no top posting in this list.
And quoting email addresses is strongly discouraged. Don't feed the spam bots.


--
With best regards,
Andrey Repin
Monday, February 10, 2020 21:44:10

Sorry for my terrible english...


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple