This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Loading cygwin1.dll from a windows app





Hi!


I am trying to load the cygwin1.dll from a windows c app, but am having 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=1.1&content-type=text/x-cvsweb-markup&cvsroot=uberbaum

I also read

http://sources.redhat.com/cgi-bin/cvsweb.cgi/winsup/testsuite/winsup.api/cygload.cc?rev=1.1&content-type=text/x-cvsweb-markup&cvsroot=uberbaum
http://sources.redhat.com/cgi-bin/cvsweb.cgi/winsup/testsuite/winsup.api/cygload.h?rev=1.2&content-type=text/x-cvsweb-markup&cvsroot=uberbaum

but I still can't make it work. = (


More detail:


I am trying to load cygwin1.dll to invoke the cygwin path conversion 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 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 java jni (java native interface) headers contain windows specific
stuff that makes gcc fail, unless -mno-cygwin is used. BTW, now that Java 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 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 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            = NULL ;
  static cygwin_conversionfunc_type cygwin_posix2win_path      = NULL ;
  static cygwin_conversionfunc_type cygwin_posix2win_path_list = NULL ;

static int mainRval ; // I declared this outside the main func 'cause I'm 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 = &pad ;
  pad.end = pad.padding + PAD_SIZE ;

  #if defined( __GNUC__ )
  __asm__ (
    "movl %%fs:4, %0"
    :"=r"( sbase )
    ) ;
  #else
  __asm {
    mov eax, fs:[ 4 ]
    mov sbase, eax
  }
  #endif
  g_pad->stackbase = sbase ;

  if ( g_pad->stackbase - g_pad->end ) {
    size_t delta = g_pad->stackbase - g_pad->end ;
    g_pad->backup = 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 = rest_of_main( argc, argv ) ;

// clean up the stack (is it necessary? we are exiting the program anyway...)
if ( g_pad->stackbase - g_pad->end ) {
size_t delta = 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  = NULL,
       *scriptpath_dyn = NULL ;
  HINSTANCE cygwinDllHandle = LoadLibrary("cygwin1.dll") ;

if ( cygwinDllHandle ) { // note that not finding cygwin1.dll is not an error - if it's not there, we are in a "normal" win cmd shell and continue w/out the path conversions

cygwin_initfunc = (cygwin_initfunc_type) GetProcAddress( cygwinDllHandle, "cygwin_dll_init" ) ;
cygwin_posix2win_path = (cygwin_conversionfunc_type)GetProcAddress( cygwinDllHandle, "cygwin_conv_to_win32_path" ) ;
cygwin_posix2win_path_list = (cygwin_conversionfunc_type)GetProcAddress( cygwinDllHandle, "cygwin_posix_to_win32_path_list" ) ;


if ( !cygwin_initfunc || !cygwin_posix2win_path || !cygwin_posix2win_path_list ) {
fprintf( stderr, "strange bug: could not find appropriate init and conversion functions inside cygwin1.dll\n" ) ;
goto end ;
}
cygwin_initfunc() ;
} // if cygwin1.dll is not found, just carry on. It means we're not 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 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/source/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/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]