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]

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


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<iostream>
//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<stdio.h> //#include<iostream> #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<stdio.h>
#include "dtest.h"
#include<Windows.h>

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 <brian@dessent.net> 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 <iostream>
>
> 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 <sys/stdio.h>, 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 <stdio.h> 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/


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