delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2005/07/12/14:02:40

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
Date: Tue, 12 Jul 2005 11:02:28 -0700 (PDT)
Message-Id: <200507121802.j6CI2Sq4005440@andrena.ucdavis.edu>
To: cygwin AT cygwin DOT com
Subject: Re: How to add 4K of scratch space at the bottom of the stack using C instead of C++?
From: "Yu-Cheng Chou" <cycchou AT ucdavis DOT edu>
X-Errors-To: cycchou AT magenta DOT ucdavis DOT edu
X-User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)
X-IsSubscribed: yes

> As has been already pointed out, you have to load cygwin1.dll first,
> initialize it, and then load your DLL.  The whole cygload thing is for
> cygwin1.dll only, it will not work for anything else.  After the Cygwin
> DLL has been loaded and initialized you should be able to load your DLL
> that depends on the Cygwin DLL normally, without any special procedure. 
> If you try to load your DLL directly Cygwin will not be initialized
> properly, and if you try to locate the Cygwin initialization routine in
> your DLL it will fail (because it exists only in the Cygwin DLL.)

My program works in this way. However, after the cygwin1.dll was 
initialized, the I/O seemed to be redirected. How can I get my I/O back?
I listed my codes below for reference.


/* main.c */
#include <stdio.h>
#include <windows.h>

extern "C" int mainCRTStartup();

extern "C" int __stdcall 
cygloadCRTStartup() {
    char padding[4096];
    return mainCRTStartup();
}

int main() {   
    char *modname = "module.dll";  
    HMODULE h;
    HMODULE handle;
    void (*init)();
    int (*fp)(int);
    int ret;

printf("1\n");

    h = LoadLibrary("cygwin1.dll");

printf("h  = %p\n", h);

    init = (void (*)())GetProcAddress(h, "cygwin_dll_init");

printf("init = %p\n", init);
 
    init(); // after this, I/O will be redirected

printf("2\n");

    handle = LoadLibrary(modname);

    if(handle == NULL) {
        fprintf(stderr, "Can't load %s in LoadLibrary()\n", modname);
        exit(1);
    }
printf("handle = %p\n", handle);

    fp = (int (*)(int))GetProcAddress(handle, "foo");

    if(fp== NULL) {
        fprintf(stderr, "ERROR: GetProcAddress()\n");
        exit(1);
    }
printf("fp = %p\n", fp);

    ret = fp(100);

printf("ret = %d\n", ret);

    return 0;
}

/* module.c */
#include <stdio.h>

int foo(int arg){
   printf("foo() is called in main.exe\n");
   printf("arg * 2 = %d\n", arg * 2);
   return arg * 2;
}

Here is the output from MS command prompt
C:\users\ycchou\VC1>main.exe
1
h  = 61000000
init = 61005790
foo() is called in main.exe
arg * 2 = 200

Thanks for anyone's help

--
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/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019