Hi,cygwin!
    pls forgive my poor english at first.

        I have compiled one sdk_lib dll using gcc in cygwin,and I writed a tool 
convert the dll to .lib so I can call the interface function in sdk_lib dll 
using vc.
Then I wrote a application using VC2005,when  it ran at beginning,everything 
looks like ok,but when I call a function "tti_readfile" which call "open write 
close" in sdk_lib many times,The application exited.By debuging,I find the  it 
crashed  in function "_aclcheck" in "cygwin1.dll".  But I found the file had 
been write success,however,after the app called it,it crashed.

who can tell me what cause this?How to resolve it?Can the dll(compiled by gcc) 
and app(compiled by vc) run in the same process? 



// so2lib.cpp : create a lib export table for vc by dll/so.
// 1.2 by phils...@techtrex.com
// modify base at a project "CodeProject Create your Proxy DLLs automatically",
// reference 《为无LIB的DLL制作LIB函数符号输入库 - VC知识库文章》
// and resolve the problem of article 《MinGW 编译的DLL在VC中使用的问题》

#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <vector>
using namespace std;

struct EXPORT_ITEM
{
        bool IsOnlyOrdinal;
        char in[300];
        char en[300];
        int o;
        int h;
};

vector<EXPORT_ITEM> v;


#pragma warning(disable : 4996)

int main(int argc, char* argv[])
{
        char szCmd[64] = {0};
        char szExportFile[64] = {0};
        char szDefFile[64] = {0};

        if (argc != 2)
        {
                printf("so2lib. Copyright (C) phils...@techtrex.com\r\n");
                printf("Usage: so2lib <so name>\r\n");
                
printf("==================================================================\r\n");
                return 0;
        }

        // STEP 1: dump exports table
        _tprintf(_T("Step 1: dump exports table %s...\r\n"),szExportFile);
        sprintf(szExportFile, "%s.txt", argv[1]);
        sprintf(szDefFile, "%s.def", argv[1]);

        sprintf(szCmd, "dumpbin /exports %s > %s", argv[1], szExportFile);
        int ret = system(szCmd);
        if (ret)
        {
                printf("Error 0: dump exports file cannot be created.pls run 
this programe in vs shell\r\n");
                system("pause");
                return 0;
        }

        // STEP 2: Read exports
        _tprintf(_T("Step 2: Parsing %s...\r\n"),szExportFile);
        FILE* fp = _tfopen(szExportFile,_T("rb"));
        if (!fp)
        {
                printf("Error 2: Txt file not found.\r\n");
                return 0;
        }
        int MSStart = 0;
        for(int i = 0 ;; )
        {
                char x[1000] = {0};
                EXPORT_ITEM e = {0};
                if (!fgets(x,1000,fp))
                        break;

                if (strnicmp(x,"EXPORT ord",10) == 0) // tdump
                {
                        printf("Error 100: error find ord!!\r\n");
                        break;
                }
                else
                {
                        if (strstr(x,"ordinal") != 0 && strstr(x,"hint") != 0 
&& strstr(x,"RVA") != 0)
                        {
                                MSStart = 1;
                                continue;
                        }
                        if (!MSStart)
                                continue;
                        char* a1 = x;
                        while(*a1 == ' ')
                                a1++;
                        if (*a1 == '\r' || *a1 == '\n')
                        {
                                if (MSStart == 1)
                                {
                                        MSStart = 2;
                                        continue;
                                }
                                break;
                        }
                        e.o = atoi(a1);
                        while(*a1 != ' ')
                                a1++;
                        while(*a1 == ' ')
                                a1++;
                        if (*a1 == '\r' || *a1 == '\n')
                        {
                                if (MSStart == 1)
                                {
                                        MSStart = 2;
                                        continue;
                                }
                                break;
                        }
                        e.h = atoi(a1);
                        while(*a1 != ' ')
                                a1++;
                        while(*a1 == ' ')
                                a1++;
                        if (*a1 == '\r' || *a1 == '\n')
                        {
                                if (MSStart == 1)
                                {
                                        MSStart = 2;
                                        continue;
                                }
                                break;
                        }
                        if (*a1 >= 0x30 && *a1 <= 0x39) // RVA exists
                        {
                                while(*a1 != ' ')
                                        a1++;
                                while(*a1 == ' ')
                                        a1++;
                                if (*a1 == '\r' || *a1 == '\n')
                                        break;
                        }

                        sprintf(e.in,"__E__%u__",i++);
                        e.IsOnlyOrdinal = false;
                        if (strnicmp(a1,"[NONAME]",8) == 0)
                        {
                                e.IsOnlyOrdinal = true;
                                sprintf(e.en,"___XXX___%u",i);
                        }
                        else
                        {
                                for(int y = 0 ; ; y++)
                                {
                                        if (*a1 == ' ' || *a1 == '\r' || *a1 == 
'\n' || *a1 == '\t')
                                                break;
                                        e.en[y] = *a1++;
                                }
                        }
                        v.insert(v.end(),e);
                }
        }
        fclose(fp);
        _tprintf(_T("Step 3: %u exported functions parsed.\r\n"),v.size());
        printf("------------------------------------------\r\n");

        _tprintf(_T("Step 4: Generating .DEF file %s...\r\n"),szDefFile);
        FILE* fdef = _tfopen(szDefFile,_T("wb"));
        if (!fdef)
        {
                printf("Error 4: DEF file cannot be created.\r\n");
                return 0;
        }

        fprintf(fdef,"LIBRARY\t%s\r\n\r\n", argv[1]);
        fprintf(fdef,"EXPORTS\r\n");
        for(unsigned int i = 0 ; i < v.size () ; i++)
        {
                fprintf(fdef,"\t%s\...@%u\r\n",v[i].en,v[i].o);
        }
        fclose(fdef);
        _tprintf(_T("Step 5: %u exported functions written to 
DEF.\r\n"),v.size());
        printf("------------------------------------------\r\n");

        _tprintf(_T("Step 6: Generating .lib file %s...\r\n"),szDefFile);
        sprintf(szCmd, "LIB /DEF:%s /MACHINE:X86", szDefFile);
        ret = system(szCmd);
        if (ret)
        {
                printf("Error 6: lib file cannot be created.\r\n");
                system("pause");
                return 0;
        }

        sprintf(szCmd, "del %s", szExportFile);
        ret = system(szCmd);

        sprintf(szCmd, "del %s", szDefFile);
        ret = system(szCmd);

        printf("success: lib file create success.\r\n");

        return 0;
}
        

Best Regards,   

phil song
phils...@techtrex.com
2010-01-29

Reply via email to