X-Spam-Check-By: sourceware.org Message-ID: <507150040705101106q30db9fdaie38ac6ea1b51af23@mail.gmail.com> Date: Thu, 10 May 2007 11:06:42 -0700 From: "kalasad mailu" To: cygwin AT cygwin DOT com Subject: Re: Please Help!! Calling Socket function in a dll file (that is created using cygwin library), by a Microsoft Visual C++ program resulting in infinite loop In-Reply-To: <46425FB7.8C763D51@dessent.net> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <507150040705021415r750600a6oc6e24b339bf57143 AT mail DOT gmail DOT com> <463909F9 DOT 6B33B9C7 AT dessent DOT net> <507150040705081919i1cbf7194y9672fc6a1b1601a2 AT mail DOT gmail DOT com> <46416B9E DOT 71C3D2C3 AT dessent DOT net> <507150040705091556y3f4842fawb084d359a83c4606 AT mail DOT gmail DOT com> <46425FB7 DOT 8C763D51 AT dessent DOT net> X-IsSubscribed: yes Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: 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 This question is also regarding my efforts to create a dll file using g++ in cygwin and use it in VC++. The below program works fine and the output is also shown below. But IF I UNCOMMENT THE TWO LINES in my file dtest.cpp i.e. //#include //std::cout<<"hello naumskara\n"; and do the same steps. Dll is created successfully, and the MSVC++ compiles fine. But when I try to run it, I get the following msg when it tries to execute the init(); (to initialize the cygwin environment) function(MSVC++ file): ------------------------------------------------------------------------------------------------------------ First-chance exception at 0x610b48b6 in new3.exe: 0xC0000005: Access violation reading location 0x00000004. ------------------------------------------------------------------------------------------------------------- I am running the MSVC++ program from the IDE. Could you please suggest me how to make the program(MSVC++) work after un-commenting the line. Thanks. ------------------------------------------------------ dtest.cpp FILE THAT I AM USING TO CREATE THE DLL FILE ------------------------------------------------------ #include //#include #define SOCKETTEST_BUILD_DLL #include "dtest.h" int called() { printf("Ctor 400 called\n"); //std::cout<<"hello naumskara\n"; return 400; } int c = called(); DLL_IMPORT_EXPORT int test_dll() { printf("%d printed from dll",c); return c; } ------------------------------------------------------ dtest.h FILE THAT I AM USING TO CREATE THE DLL FILE ------------------------------------------------------ #ifdef SOCKETTEST_BUILD_DLL #define DLL_IMPORT_EXPORT __declspec(dllexport) #else #define DLL_IMPORT_EXPORT __declspec(dllimport) #endif extern "C" { DLL_IMPORT_EXPORT int test_dll(); } ------------------------------------------------------- COMMANDS THAT I USED TO CREATE THE DLL AND LIB FILES ------------------------------------------------------- $ g++ -shared -o dtestdll.dll dtest.cpp -Wl,--output-def,dtestdll.def,--out-implib,libdtestdll.a C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\bin\lib" /machine:i386 /DEF:dtestdll.def ------------------------------------------------------------------ MSVC++ PROGRAM USING THE DLL FILE, THAT IS CREATED IN CYGWIN USING g++ ------------------------------------------------------------------ #include #include "dtest.h" #include void main(){ HMODULE h = LoadLibrary("cygwin1.dll"); void (*init)() = (void(__cdecl *)(void))GetProcAddress(h, "cygwin_dll_init"); init(); int k= test_dll(); printf("\nthe OSA value returned is %d\n", k); Sleep(5000); } --------------------------------- OUTPUT FROM MY MSVC++ PROGRAM: -------------------------------- Ctor 400 called 400 printed from dll the OSA value returned is 400 On 5/9/07, Brian Dessent wrote: > kalasad mailu wrote: > > > I used the socket.h file provided by cygwin(/usr/include/cygwin/.) and > > wrote a program to create socket, made this a dll file and used this > > dll in VC++. > > > > I assume using of the socket.h( form cygwin) did all the conversion > > form the linux system calls to the windows system calls and made my > > socket program work. Please correct me if my understanding is wrong. > > A header file does not implement anything. It contains no code at all > (except in the case of C++ or inlined functions.) It only describes an > interface that is actually implemented in a library. In the case of > Cygwin, the standard C library is implemented by cygwin1.dll, along with > many POSIX functions. > > > Now I want to try the same process for simple c++ functions like > > "cout". I don't find any cygwin header files like iostream.h or > > stream.h in the cygwin directory. > > cout is part of the C++ standard library (STL) and is implemented by > libstdc++ which is part of gcc. > > > When I created this stand alone program. > > > > #include > > > > int main () { > > std::cout << "Hello World\n"; > > return 0; > > } > > > > I guess this picked the header file from > > "cygwin\lib\gcc\i686-pc-cygwin\3.4.4\include\c++\" (a gcc header file) > > > > Is there no iostream header file by cygwin (/usr/include/cygwin)? > > First of all, stop looking in /usr/include/cygwin for things, and stop > worrying about which directory header files are in. That has no bearing > on what library implements a specific function, as all headers for all > installed libraries are in /usr/include. Some of these headers are > provided by Cygwin. The ones in the "cygwin" subdirectory are only for > Cygwin-specific things, but this is only a small fraction of the > functionality provided by Cygwin. For example, all of the stadard C > library functions (such as stdio.h, stdlib.h, io.h, stdint.h, and on and > on) are all implemented by Cygwin, i.e. cygwin1.dll, and are in > /usr/include. And many other libraries as well, such as zlib, gettext, > libintl, and so on. So, stop confusing a header file with the library > that implements the code and realize that the location a header file > resides on disk has no relationship at all to the implementation. > > > I am running into header file conflict when I also include both cygwin > > header files and the gcc header files. How to solve the conflict? > > No, there's no conflict. Gcc implements some libraries of its own, and > Cygwin provides most of the standard C library. > > > Is it possible to make a dll that uses the functions defined by the > > gcc header file(***not the cygwin header file i.e. present in > > (/usr/include/cygwin)***) and use it in the MSVC++? > > Stop thinking of things by directory! Look at the headers in the cygwin > package (cygcheck -l cygwin). This represents everything implemented by > cygwin1.dll. See that there are many things outside of > /usr/include/cygwin. > > Now look at the libraries provided by gcc for the C++ runtime (cygcheck > -l gcc-g++). See that gcc implements most of the C++ runtime > functionality. There are all designed to perfectly coexist, there is no > problem with using both in a given program or DLL. > > As to using something in MSVC, first of all libstdc++ is gcc specific, > so there is no way to use it at the source level with anything else. > YOu can link to it at the binary level, i.e. call code written by g++ > from MSVC, but only if the interface is 'extern "C"', because the two > compilers have different C++ ABIs which cannot coexist. > > > If including the header file from gcc works then why is cygwin > > providing separate header files(like stdio.h, stdlib.h and etc in > > /usr/include/cygwin)? > > Different headers provide different things. /usr/include/stdio.h > provides the standard C library interface. /usr/include/sys/stdio.h > provides low level implementation details. You do not normally write > #include , as it is included by other headers > automatically. Stop focusing on the location of things, and think of it > as an interface. When you use #include you are using a > certain interface; the details of how it is implemented are not your > concern. stdio.h could include /usr/include/foo/bar/baz.h if it wanted > to. > > Brian > > -- > 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/ > > -- 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/