Thread: Creating DLL in MS VS 6.0 in C

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    38

    Creating DLL in MS VS 6.0 in C

    Using Visual Studio 6 C++ (but programming C)

    1. Have created new project of type Win32 Dynamic Link Library.

    2. Have got all c code (i.e. imported from standard project I made earlier), headers and external dependancies set up and imported.

    3. Have turned of precompiled headers.

    4. In main () I have included:

    Code:
    #include <windows.h>
    #include <dos.h>
    5. I have been following instuctions as outlined in this web link
    http://support.microsoft.com/default...b;en-us;106553

    I cant go any further in this link when I try to compile because I get these strange compile errors.

    Code:
    c:\program files\microsoft visual studio\vc98\include\windef.h(43) : warning C4114: same type qualifier used more than once
    c:\program files\microsoft visual studio\vc98\include\windef.h(43) : error C2632: 'long' followed by 'long' is illegal
    c:\program files\microsoft visual studio\vc98\include\windef.h(43) : warning C4091: 'typedef ' : ignored on left of 'unsigned long ' when no variable is declared
    c:\program files\microsoft visual studio\vc98\include\windef.h(45) : warning C4114: same type qualifier used more than once
    c:\program files\microsoft visual studio\vc98\include\windef.h(45) : error C2632: 'short' followed by 'short' is illegal
    c:\program files\microsoft visual studio\vc98\include\windef.h(45) : warning C4091: 'typedef ' : ignored on left of 'unsigned short ' when no variable is declared...........
    and so on..

    Does anyone have any any clear and comprehensive instuctions, web links etc on how you accomplish making dll's in this envionment - as I think the link I am following is out of date (and a little confusing, as I have never created a dll before).

    Many thanks
    Simon
    Last edited by earnshaw; 01-12-2005 at 11:44 AM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    It looks like you are trying to compile C code as C++.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    38
    This is C code, and I want instructions to compile a DLL in C - but I'm using Visual C++, because you can make it compile in C - and I have successfully made C programs in the past using this method, so I am a bit reluctant to try other compilers.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by earnshaw
    Using Visual Studio 6 C++ (but programming C)


    I cant go any further in this link when I try to compile because I get these strange compile errors.

    Code:
    c:\program files\microsoft visual studio\vc98\include\windef.h(43) : warning C4114: same type qualifier used more than once
    c:\program files\microsoft visual studio\vc98\include\windef.h(43) : error C2632: 'long' followed by 'long' is illegal
    and so on..

    Does anyone have any any clear and comprehensive instuctions, web links etc on how you accomplish making dll's in this envionment - as I think the link I am following is out of date (and a little confusing, as I have never created a dll before).

    Many thanks
    Simon
    Visual Studio 6 doesn't have long long data types (at least mine doesn't).
    You might want to get Visual C++ 2005 Express. (Free download).

    If you want to use VS6, the data type is __int64:

    Code:
    /* use of 64-bit ints with Microsoft Visual Studio 6 */
    #include <stdio.h>                                    
    typedef unsigned __int64 Long; /* typedef for my convenience */
    int main()                                       
    {                                                     
       Long juul = 123456789012345678L;    
    
       printf("sizeof(Long) = %d\n", sizeof (Long));
       printf("juul: %I64d (decimal)\n",juul);
       printf("juul: 0x%016I64X (hex)\n",juul);                     
       return 0;
    }
    Regards,

    Dave
    Last edited by Dave Evans; 01-12-2005 at 03:38 PM.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Download the newest version of the platform SDK, and use those header files instead. My version of windef.h compiles just fine with visual c++.

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    38
    I found that I could compile ok a C++ dll (no surprise there). I was just testing it and seeing what was involved. I then tried to compile the same code, but as C, and it didnt work, which didnt surprise me really, but I was holding out for some simple errors. However the errors seem odd and unfixable, so I must have to change some things in the header file I think the DEF file I have will be ok for Visual Basic 6.

    Here is an example of the errors.

    Code:
    Compiling...
    c_dll_4_vb.c
    c:\cprogs\c_dll_4_vb\c_dll_4_vb.h(9) : error C2143: syntax error : missing ')' before '&'
    c:\cprogs\c_dll_4_vb\c_dll_4_vb.h(9) : error C2143: syntax error : missing '{' before '&'
    c:\cprogs\c_dll_4_vb\c_dll_4_vb.h(9) : error C2059: syntax error : '&'
    c:\cprogs\c_dll_4_vb\c_dll_4_vb.h(9) : error C2059: syntax error : ')'
    c:\cprogs\c_dll_4_vb\c_dll_4_vb.c(5) : error C2143: syntax error : missing ')' before '&'
    c:\cprogs\c_dll_4_vb\c_dll_4_vb.c(5) : error C2143: syntax error : missing '{' before '&'
    c:\cprogs\c_dll_4_vb\c_dll_4_vb.c(5) : error C2059: syntax error : '&'
    c:\cprogs\c_dll_4_vb\c_dll_4_vb.c(5) : error C2059: syntax error : ')'
    Error executing cl.exe.
    I suppose the question I should be asking is what do I need in the header file and what format should the C file be in e.g. what keywords, includes should I be using, also are there any settings I should configure in the Project->Settings?

    Thanks

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Post the contents of c_dll_4_vb.h and c_dll_4_vb.c

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    38
    This is the c code


    #include "c_dll_4_vb.h"


    Code:
    long __stdcall Resolve_Name_To_Ip
      (char *pcDomainToResolve, char szReturnIP[500], int &iSize)
    {
    	  WSADATA		wsaData;					
    	  LPTSTR		CompName = new char[255];
    	  LPDWORD		CompSize = new unsigned long(255);
    
    	  struct		sockaddr_in dest;
    	  struct		hostent *hp;
    	  char			*dest_ip;
    
    	  unsigned int	addr = 0;
    
    	  strcpy(CompName,pcDomainToResolve);
    
    	  if(WSAStartup(MAKEWORD(2,1), &wsaData) != 0)
    	  {  WSACleanup();
    		 return -100;
    	  }
    
    	  hp = gethostbyname(CompName);
    	  if(!hp)
    	  {  addr = inet_addr(CompName);	
    	  }
    
    	  if((!hp) && (addr == INADDR_NONE))
    	  {// Unable to resolve domain ip.
    		WSACleanup();
    		return -200;
    	  }
    
    	  hp = gethostbyname(CompName);
    	  if(hp != NULL)
    		memcpy(&(dest.sin_addr),hp->h_addr,hp->h_length);
    	  else
    		dest.sin_addr.s_addr = addr;
    
    	  if(hp)
    		dest.sin_family = hp->h_addrtype;
    	  else
    		dest.sin_family = AF_INET;
    
    	  dest_ip = inet_ntoa(dest.sin_addr);
    		
    	  iSize = strlen(dest_ip);
    		
    	  // Allow the string to return to the proper size.
    	  strncpy(szReturnIP, dest_ip, strlen(dest_ip)); 
    		
    	  dest_ip = NULL;
    	  hp = NULL;
    	  addr = 0;
    	  dest.sin_family = NULL;
    	  dest.sin_addr.s_addr = NULL;	
    	  dest.sin_addr.S_un.S_addr = NULL;
    	  dest.sin_port = NULL;
    
    	  delete [] CompName;
    	  delete [] CompSize;
    
    	  WSACleanup();
    	  return (0);
    }

    This is the header:

    Code:
    #include <windows.h>
    #include <winsock.h>
    #include <stdio.h>
    
    // Function is used to resolve a domain name to an IP address.
    // The return values are: -100 = Incorrect version of Winsock
    //                        -200 = Cant resolve domain.
    long __stdcall Resolve_Name_To_Ip
        (char *pcDomainToResolve, char szReturnIP[500], int &iSize);

  9. #9
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    That isn't C code. C doesn't have new or delete, and I believe it doesn't have references. Try this:
    Code:
    In c_dll_4_vb.c
    long __stdcall Resolve_Name_To_Ip
      (char *pcDomainToResolve, char szReturnIP[500], int* iSize)
    {
    	  WSADATA		wsaData;					
    	  //LPTSTR		CompName = new char[255];
    	  LPTSTR		CompName = malloc(sizeof(char)*255);
    	  //LPDWORD		CompSize = new unsigned long(255);
    	  LPDWORD		CompSize = malloc(sizeof(unsigned long)*255);
    
    	  struct		sockaddr_in dest;
    	  struct		hostent *hp;
    	  char			*dest_ip;
    
    	  unsigned int	addr = 0;
    
    	  strcpy(CompName,pcDomainToResolve);
    
    	  if(WSAStartup(MAKEWORD(2,1), &wsaData) != 0)
    	  {  WSACleanup();
    		 return -100;
    	  }
    
    	  hp = gethostbyname(CompName);
    	  if(!hp)
    	  {  addr = inet_addr(CompName);	
    	  }
    
    	  if((!hp) && (addr == INADDR_NONE))
    	  {// Unable to resolve domain ip.
    		WSACleanup();
    		return -200;
    	  }
    
    	  hp = gethostbyname(CompName);
    	  if(hp != NULL)
    		memcpy(&(dest.sin_addr),hp->h_addr,hp->h_length);
    	  else
    		dest.sin_addr.s_addr = addr;
    
    	  if(hp)
    		dest.sin_family = hp->h_addrtype;
    	  else
    		dest.sin_family = AF_INET;
    
    	  dest_ip = inet_ntoa(dest.sin_addr);
    		
    	  *iSize = strlen(dest_ip);
    		
    	  // Allow the string to return to the proper size.
    	  strncpy(szReturnIP, dest_ip, strlen(dest_ip)); 
    		
    	  dest_ip = NULL;
    	  hp = NULL;
    	  addr = 0;
    	  //dest.sin_family = NULL;
    	  //dest.sin_addr.s_addr = NULL;	
    	  //dest.sin_addr.S_un.S_addr = NULL;
    	  //dest.sin_port = NULL;
    	  dest.sin_family = 0;
    	  dest.sin_addr.s_addr = 0;	
    	  dest.sin_addr.S_un.S_addr = 0;
    	  dest.sin_port = 0;
    
    	  free(CompName);
    	  free(CompSize);
    
    	  WSACleanup();
    	  return (0);
    }
    
    In c_dll_4_vb.h
    #include <windows.h>
    #include <winsock.h>
    #include <stdio.h>
    
    // Function is used to resolve a domain name to an IP address.
    // The return values are: -100 = Incorrect version of Winsock
    //                        -200 = Cant resolve domain.
    long __stdcall Resolve_Name_To_Ip
        (char *pcDomainToResolve, char szReturnIP[500], int *iSize);
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  10. #10
    Registered User
    Join Date
    Dec 2004
    Posts
    38
    Have compiled succesfully in C now!
    Out of the code posted here - and I dont fully understand it all, especially WSAStartup - what are the essential prerequisites in order to get any program you write (in C) to work as a dll?

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    2
    WSAStartup : Winsock Addition Startup, inits ws_32.dll no relation to coding a dll but using one.

  12. #12
    Registered User
    Join Date
    Dec 2004
    Posts
    38
    Thanks RezaK

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating a simple DLL
    By Dark_Phoenix in forum C++ Programming
    Replies: 8
    Last Post: 03-07-2009, 09:41 PM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  3. Speed test result
    By audinue in forum C Programming
    Replies: 4
    Last Post: 07-07-2008, 05:18 AM
  4. Ping
    By ZakkWylde969 in forum Tech Board
    Replies: 5
    Last Post: 09-23-2003, 12:28 PM
  5. The Timing is incorret
    By Drew in forum C++ Programming
    Replies: 5
    Last Post: 08-28-2003, 04:57 PM