X-Spam-Check-By: sourceware.org To: cygwin AT cygwin DOT com From: "Antti Karanta" Subject: Loading cygwin1.dll from a windows app Date: Mon, 26 Mar 2007 15:20:00 +0300 Lines: 198 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; format=flowed; delsp=yes; charset=iso-8859-15 Content-Transfer-Encoding: Quoted-Printable User-Agent: Opera Mail/9.10 (Win32) X-IsSubscribed: yes Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT cygwin DOT com Mail-Followup-To: cygwin AT cygwin DOT com Delivered-To: mailing list cygwin AT cygwin DOT com Hi! I am trying to load the cygwin1.dll from a windows c app, but am having= =20=20 difficulties. I did a fair amount of googling and found stuff concerning this, e.g. http://cygwin.com/faq/faq.programming.html#faq.programming.msvs-mingw http://sources.redhat.com/cgi-bin/cvsweb.cgi/winsup/cygwin/how-cygtls-works= .txt?rev=3D1.1&content-type=3Dtext/x-cvsweb-markup&cvsroot=3Duberbaum I also read http://sources.redhat.com/cgi-bin/cvsweb.cgi/winsup/testsuite/winsup.api/cy= gload.cc?rev=3D1.1&content-type=3Dtext/x-cvsweb-markup&cvsroot=3Duberbaum http://sources.redhat.com/cgi-bin/cvsweb.cgi/winsup/testsuite/winsup.api/cy= gload.h?rev=3D1.2&content-type=3Dtext/x-cvsweb-markup&cvsroot=3Duberbaum but I still can't make it work. =3D ( More detail: I am trying to load cygwin1.dll to invoke the cygwin path conversion=20= =20 functions (to make my program easier to use from cygwin shell). I do this to convert the command line params denoting paths or files to=20= =20 windows format that the java virtual machine I am launching expects. The jvm is also the reason why I can't compile my app as cygwin app - the= =20=20 java jni (java native interface) headers contain windows specific stuff that makes gcc fail, unless -mno-cygwin is used. BTW, now that Java= =20=20 is gpl are there any plans to make a cygwin version of Java? That would naturally solve my problem, too, as I could compile my app as a cygwin app= =20=20 and wouldn't even need to call the path conversion funcs. The example about loading cygwin1.dll in a win app was in c++, but my=20= =20 code is c. Also, I am not familiar w/ assembler programming, so I suspect it is one of these my error lies. Anyhow, here's what I am doing now: typedef void ( *cygwin_initfunc_type)() ; typedef void ( *cygwin_conversionfunc_type)( const char*, char*) ; static cygwin_initfunc_type cygwin_initfunc =3D NULL ; static cygwin_conversionfunc_type cygwin_posix2win_path =3D NULL ; static cygwin_conversionfunc_type cygwin_posix2win_path_list =3D NULL ; static int mainRval ; // I declared this outside the main func 'cause I'm= =20=20 a little unsure of what the stack manipulation // does // 2**15 #define PAD_SIZE 32768 typedef struct { void* backup ; void* stackbase ; void* end ; byte padding[ PAD_SIZE ] ; } CygPadding ; static CygPadding *g_pad ; // ... // in main: CygPadding pad ; void* sbase ; g_pad =3D &pad ; pad.end =3D pad.padding + PAD_SIZE ; #if defined( __GNUC__ ) __asm__ ( "movl %%fs:4, %0" :"=3Dr"( sbase ) ) ; #else __asm { mov eax, fs:[ 4 ] mov sbase, eax } #endif g_pad->stackbase =3D sbase ; if ( g_pad->stackbase - g_pad->end ) { size_t delta =3D g_pad->stackbase - g_pad->end ; g_pad->backup =3D malloc( delta ) ; if( !( g_pad->backup) ) { fprintf( stderr, "error: out of mem when copying stack state\n" ) ; return -1 ; } memcpy( g_pad->backup, g_pad->end, delta ) ; } mainRval =3D rest_of_main( argc, argv ) ; // clean up the stack (is it necessary? we are exiting the program=20=20 anyway...) if ( g_pad->stackbase - g_pad->end ) { size_t delta =3D g_pad->stackbase - g_pad->end ; memcpy( g_pad->end, g_pad->backup, delta ) ; free( g_pad->backup ) ; } // ... // in rest_of_main invoked above: char *classpath_dyn =3D NULL, *scriptpath_dyn =3D NULL ; HINSTANCE cygwinDllHandle =3D LoadLibrary("cygwin1.dll") ; if ( cygwinDllHandle ) { // note that not finding cygwin1.dll is not an= =20=20 error - if it's not there, we are in a "normal" win cmd shell and continue= =20=20 w/out the path conversions cygwin_initfunc =3D (cygwin_initfunc_type)=20=20=20=20=20= =20=20 GetProcAddress( cygwinDllHandle, "cygwin_dll_init" ) ; cygwin_posix2win_path =3D=20=20 (cygwin_conversionfunc_type)GetProcAddress( cygwinDllHandle,=20=20 "cygwin_conv_to_win32_path" ) ; cygwin_posix2win_path_list =3D=20=20 (cygwin_conversionfunc_type)GetProcAddress( cygwinDllHandle,=20=20 "cygwin_posix_to_win32_path_list" ) ; if ( !cygwin_initfunc || !cygwin_posix2win_path ||=20=20 !cygwin_posix2win_path_list ) { fprintf( stderr, "strange bug: could not find appropriate init and= =20=20 conversion functions inside cygwin1.dll\n" ) ; goto end ; } cygwin_initfunc() ; } // if cygwin1.dll is not found, just carry on. It means we're not=20= =20 inside cygwin shell and don't need to care about cygwin path conversions Something bad evidently happens as stuff written to stdout by the jvm=20= =20 goes nowhere - it does not appear on screen. Any ideas? You can find the full source at: https://svn.codehaus.org/groovy/trunk/groovy/modules/native_launcher/sou= rce/groovy.c -Antti- Ps. My environment is win xp sp 2 and $ uname -a CYGWIN_NT-5.1 NW17 1.5.23(0.156/4/2) 2006-12-19 10:52 i686 Cygwin -- Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple Problem reports: http://cygwin.com/problems.html Documentation: http://cygwin.com/docs.html FAQ: http://cygwin.com/faq/