Solved

how to make a simble dll?

Posted on 2001-06-29
34
401 Views
Last Modified: 2013-12-14
hello,
i what to make a simple dll without MFC in vc6.0.
can someone give's me a example of a calulation function in a dll or something else were i can learn from?
or maybe a compleyd documetation?
every thing is welcome.

plz help
carlos smith
0
Comment
Question by:krydea
  • 18
  • 9
  • 5
  • +2
34 Comments
 
LVL 3

Expert Comment

by:MDarling
Comment Utility
hi,

generate a new project with app wizard based on "Win32 Dynamic Link Library"

Don't let it generate any files for you.

Then when the project is generated add a new file (main.cpp) containing the following.

#include <windows.h>

BOOL WINAPI DllMain(HANDLE hInstance,DWORD dwReason,LPVOID)
{
    switch(dwReason)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_PROCESS_DETACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
        break;
    }
    return TRUE;
}

// an exported function - extern "C" removes C++ name mangling
extern "C" int __declspec(dllexport) AddTwoNumbers(int a,int b)
{
    return a+b;
}


Now generate a console application containing the following...

#include <windows.h>
#include <stdio.h>

typedef int (*tpfnSumFunction)(int ,int);

void main()
{
    // load the dll
    HMODULE hLib=LoadLibrary("ee2.dll");
    if(hLib)
    {
        //get the address of our function

        tpfnSumFunction pfnSumFunction=(tpfnSumFunction )GetProcAddress(hLib,"AddTwoNumbers");
        if(pfnSumFunction)
        {
            printf("3+4=%d\n",pfnSumFunction(3,4));
        }
        else
        {
            printf("Couldn't get address of function\n");
        }
        // unload the dll
        FreeLibrary(hLib);
    }
    else
    {
        puts("Couldn't load library.");
    }

}


Now just compile the 2 projects - copy the exe and dll into the same directory and run the exe.

Notice the extern "C" stuff on the exported function - this is to stop C++ name mangling and make things easier for you.

If we don't do this the function would be exported something like this...

?AddTwoNumbers@@YAHHH@Z

Which is not easy to remember :)

Regards,
Mike.



0
 
LVL 10

Expert Comment

by:makerp
Comment Utility
0
 
LVL 4

Expert Comment

by:prashant_n_mhatre
Comment Utility
0
 

Author Comment

by:krydea
Comment Utility
can you help?
0
 
LVL 4

Expert Comment

by:prashant_n_mhatre
Comment Utility
Here is the complete exmple in plain English. If you follow the instructions, you should be able to do this.

With VC++ v6 , create a new Win32 Dynamic DLL project and name it : howto

Specify that it is a DLL that exports some symbols

Edit the howto.cpp file.
Add the following function :        HOWTO_API char * __stdcall sayhello(void)
       {
           return "hello";
       }
 
Edit the howto.h file.
Add the following line :          HOWTO_API char * __stdcall sayhello(void);
 
In the Project settings for Win32 release.
Make sure that you are not using the MFC in General Tab.
In the Link tab, category General, check the option Generate MAP file.
Click the OK button.

From the Build menu, set the active configuration to Win 32 release.
Build the project.

Open the file howto.map located in the Release directory.
Find the sayhello entry and note the "decorated name".
In this example, it's ?sayhello@@YGPADXZ

Click on Source files (in the treeview) and from Menu choose New.
Select Text file, check Add to the project and give howto.def as the name.
Enter the following lines in the new howto.def file :          ; DEF file for the  Howto DLL
           EXPORTS
               sayhello =?sayhello@@YGPADXZ

note: this will add /def:".\howto.def" to the Project options (you can see these options in Project settings).

Save everything and build the DLL





0
 
LVL 4

Expert Comment

by:prashant_n_mhatre
Comment Utility
You may ignore the rest of the part after building dll.
It is done for exporting it to other application.


0
 
LVL 4

Expert Comment

by:prashant_n_mhatre
Comment Utility
And here are the projects with documentation you are looking for.

http://cs.ua.edu/components/
0
 

Author Comment

by:krydea
Comment Utility
can i mail someone i thing i what to add to a dll?
and can you try to do it. and explane what you did...
plz..?
0
 

Author Comment

by:krydea
Comment Utility
only one else it will be to mutch work for you guy's..
prashant_n_mhatre? can you plz try?
0
 
LVL 1

Expert Comment

by:ua1zcl
Comment Utility
Give me your project by e-mail, please.
Alex ua1zcl@mail.ru
0
 
LVL 1

Expert Comment

by:ua1zcl
Comment Utility
// krydea's task he posted me:

// you can just compile this source you have to creat at furst a Hello World
// app with mvc6.0 when you have done that the Hello World source have to be
// Deleted and put this in the place of that..  now you have to set some setting's
// all of this is in the Alt-F7 menu..... add ws2_32.lib to this project before
// compile and set it on multithreaded.(krydea wrote)

// ua1zcl: it's console project, i excluded stdafx.h - not need as i know
// added files to project : msvcrt.lib , Ws2_32.lib
// to Project setting - Linker - Project options was added  /nodefaultlib:"LIBCD"
// built by VC5 but VC6 was required
//(required files from VC6 (acssvcc.h ...) i inserted from distribution disk)
// Hello.exe - 0 error(s), 0 warning(s)
// Becuse krydea not gave me e-mail adr, i decided to paste this here.
// I not understand about what dll we are busy here ...?
//////////////////////////////////////////////////////////////////////////////////
// hello.cpp
#define _MT // multithread for process.h
#include <process.h>
#include <winsock2.h>
//#include <windows.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <acssvcc.h>


void WaitForConnect(void* pParam);
void WaitForReceive(void* pParam);
char* ArgFile=NULL;
bool WillSnd=false;
bool InitReady=false;
bool CloseNow=false;

class CSocket
{
public:
   CSocket()
   {
      Handle=INVALID_SOCKET;
      Client=NULL;
      Init=Server=Active=FALSE;
   };

   ~CSocket(){};
   BOOL Open(int af = AF_INET, int type = SOCK_STREAM, int protocol = 0)
   {
     if( !Init )
     {
      {
        WSADATA WSAData;
        if( WSAStartup(MAKEWORD(1,1), &WSAData) != 0 )return(FALSE);
      }
         Init=TRUE;
     }
      Handle=socket(af, type, protocol);
      if( Handle == INVALID_SOCKET )return(FALSE);
      return(TRUE);
   }

   BOOL Initialize(char* Address, WORD Port)
   {
      HOSTENT hAddress;
      SOCKADDR_IN hSockAddr;
      if( !FillAddress(Address, &hAddress) )return(FALSE);
      memcpy((char FAR *)&(hSockAddr.sin_addr), hAddress.h_addr, hAddress.h_length);
      hSockAddr.sin_family=AF_INET;
      hSockAddr.sin_port=Port;
      if( bind(Handle, (struct sockaddr FAR *)&hSockAddr, sizeof(hSockAddr)) ==
      SOCKET_ERROR )
      return(FALSE);
      return(TRUE);
   }

   BOOL StartServer(char* Addr, WORD Port)
   {
    if( !Initialize(Addr, Port) )return(FALSE);
    if( listen(Handle, 1) == SOCKET_ERROR )return(FALSE);
    Server=Active=TRUE;
    WaitThread=_beginthread(WaitForConnect, 0, (void* )this);
    return(TRUE);
   }

   BOOL StartClient(char* Addr, WORD Port)
   {
    HOSTENT hAddress;
    SOCKADDR_IN hSockAddr;
    if( !FillAddress(Addr, &hAddress) )return(FALSE);
    memcpy((char FAR *)&(hSockAddr.sin_addr), hAddress.h_addr, hAddress.h_length);
    hSockAddr.sin_family=AF_INET;
    hSockAddr.sin_port=Port;
    if( connect(Handle, (struct sockaddr FAR *)&hSockAddr, sizeof(hSockAddr)) ==
       SOCKET_ERROR )
    return(FALSE);
    WaitThread=_beginthread(WaitForReceive, 0, (void* )this);
    return(TRUE);
   }

   BOOL Close(void)
   {
     if( Client )closesocket(Client);
     if( Handle )closesocket(Handle);
     Client=Handle=0;
     Init=Active=FALSE;
     return(TRUE);
   }

   BOOL SendText(char* Text)
   {
    if( send(Handle, Text, strlen(Text)+1, MSG_DONTROUTE) != SOCKET_ERROR )
    return(FALSE);
    return(TRUE);
   }

   BOOL SendFile(char* Text)
   {
    FILE* File;
    printf("\nSend: %s\n", Text);
    File=fopen(Text, "rb");
    if( !File )return(FALSE);
    char Buffer[1024]="@";
    send(Handle, Buffer, 1, MSG_DONTROUTE);
    fseek(File, 0, SEEK_END);
    long Size=ftell(File);
    fseek(File, 0, SEEK_SET);
    sprintf(Buffer, "%10.10ld", Size);
    send(Handle, Buffer, 11, MSG_DONTROUTE);
    int Len;
    while( (Len=fread(Buffer, 1, 1024, File)) > 0 )
    send(Handle, Buffer, Len, MSG_DONTROUTE);
    fclose(File);
    return(TRUE);
   }

   BOOL FillAddress(char* Address, HOSTENT* hAddress)
   {
    HOSTENT* p=gethostbyname(Address);
    if( !p ) return(FALSE);
    memcpy(hAddress, p, sizeof(HOSTENT));
    return(TRUE);
   }

   void ErrorFunction(char* Title = NULL)
   {
    int Error=WSAGetLastError();
    char* Msg;
    switch( Error )
    {
     case WSANOTINITIALISED:    
           Msg="A successful AfxSocketInit must occurbefore using this API."; break;
     case WSAENETDOWN :    
         Msg="The Windows Sockets implementationdetected that the network "
                  "subsystem failed."; break;
     case WSAEADDRINUSE:  Msg="The specified address is already inuse."; break;
     case WSAEINPROGRESS: Msg="A blocking Windows Sockets call is inprogress.";
     break;
     case WSAEADDRNOTAVAIL :
          Msg="The specified address is not availablefrom the local machine."; break;
     case WSAEAFNOSUPPORT  :    
         Msg="Addresses in the specified familycannot be used with this socket.";
        break;
     case WSAECONNREFUSED:    Msg="The attempt to connect was rejected.";break;
     case WSAEDESTADDRREQ  :    Msg="A destination address is required.";break;
     case WSAEFAULT:    Msg="The nSockAddrLen argument isincorrect."; break;
     case WSAEINVAL:Msg="The socket is not already bound to anaddress."; break;
     case WSAEISCONN  :    Msg="The socket is already connected.";break;
     case WSAEMFILE   :    Msg="No more file descriptors areavailable."; break;
     case WSAENETUNREACH:  
         Msg="The network cannot be reached from thishost at this time."; break;
     case WSAENOBUFS :
          Msg="No buffer space is available. Thesocket cannot be connected."; break;
     case WSAENOTSOCK      :    Msg="The descriptor is not a socket.";break;
     case WSAETIMEDOUT :    
          Msg="Attempt to connect timed out withoutestablishing a connection."; break;
     case WSAEWOULDBLOCK   :    
          Msg="The socket is marked as nonblocking andthe connection cannot"
                   " be completed immediately."; break;
     default  :  Msg="Unknown error."; break;
    }// switch

   if( Title )printf("\r\n%s\r\n%s\r\n", Title, Msg);
   else printf("\r\n%s\r\n", Msg);
   CloseNow=true;
  }//void ErrorFunction(char* Title = NULL) end
///////////////////////////////////////////////////////////////////////
 
   BOOL Init;
   BOOL Server;
   BOOL Active;
   SOCKET Handle;
   SOCKET Client;
   unsigned long WaitThread;
};// class CSocket ends
/////////////////////////////////////////////////////////////////////////////////
 

void WaitForConnect(void* pParam)
{
 CSocket* Socket=(CSocket* )pParam;
 Socket->Client=accept(Socket->Handle, NULL, NULL);

 if( Socket->Client != INVALID_SOCKET )
 {
  printf("\r\nConnected\r\n");
  Socket->Server=FALSE;
  closesocket(Socket->Handle);
  Socket->Handle=Socket->Client;
  Socket->Client=0;
  Socket->WaitThread=_beginthread(WaitForReceive, 0, (void* )Socket);
 }
 else
 {
   printf("\r\nNot Connected\r\n");
   Socket->Client=0;
   Socket->WaitThread=NULL;
 }
  _endthread();
}
/////////////////////////////////////////////////////////////////////////////
 

void WaitForReceive(void* pParam)
{
 CSocket* Socket=(CSocket* )pParam;        
 if (!WillSnd)InitReady=true;
 int Ret;
 char Buffer;

  while( (Ret=recv(Socket->Handle, &Buffer, 1, 0) != SOCKET_ERROR) && Ret )
 {
  if( Buffer == '@' ) // File
  {
    printf("\nRecieve: %s\n", ArgFile);
    FILE* File;
    long nSize, S;
    char Size[11];
    recv(Socket->Handle, Size, 11, 0);
    S=nSize=atol(Size);
    printf("File: %-10.10ld\r\n", nSize);
    printf("%-10.10ld", 0l);
    File=fopen(ArgFile, "wb");

    while( nSize)
   {
    static char Buffer[1024]="";
    long BuffSize=1024;

    if( nSize < BuffSize )BuffSize=nSize;
    BuffSize=recv(Socket->Handle, Buffer, BuffSize, 0);
    fwrite(Buffer, 1, BuffSize, File);
    nSize-=BuffSize;
    printf("\b\b\b\b\b\b\b\b\b\b");
    printf("%-10.10ld", S-nSize);
   }

   fclose(File);
   printf(" : OK\r\n");
   CloseNow=true;
  }// if
  else if( Buffer == 'G' ) // Go send
  {
    InitReady=true;
  }
 }// while 1

 CloseNow=true;
 printf("\r\nNot Connected\r\n");
 Socket->WaitThread=NULL;
 _endthread();
}
//////////////////////////////////////////////////////////////////////////////
 

int main(int argc, char* argv[])
{
 CSocket Socket;
 char* ArgIP=NULL;
 char* ArgSR=NULL;
 
  if( argc == 4 )
  {
   ArgIP=argv[1];
   ArgFile=argv[3];
   WillSnd=true;
   printf("You will send the file %s to %s\n", ArgFile, ArgIP);
   if( !Socket.Open() || !Socket.StartServer(ArgIP, 99) )
   {
     Socket.ErrorFunction();
   }
   else
   printf("\nPress Ctrl+C to exit\nWait...\n");

   while(1)
   {
     if (InitReady)
     {
      InitReady=false;
      printf("Server Init Completed\n");
      if (Socket.SendFile(ArgFile))
      {
        printf("DONE\n");
      }
      else
      {
       printf("ERROR\n");
      }

     }

     if (CloseNow)
     {
       if (Socket.Init)Socket.Close();
       return(1);
     }
   }// while ends

   return(1);
  }// if( argc == 4 ) ends

   if( argc == 3 )
   {
    ArgIP=argv[1];
    ArgFile=argv[2];
    WillSnd=false;
    printf("You will recieve a file from %s and will save it as %s\n"
                    , ArgIP, ArgFile);  
    if( !Socket.Open() || !Socket.StartClient(ArgIP, 99) )
    {
      Socket.ErrorFunction();
    }
    else printf("\r\nConnected\r\nPress Q<enter> to exit\n");
 
    while(1)
    {                  
      if (InitReady)
      {
       InitReady=false;
       printf("Client Init Completed\n");
       Socket.SendText("G");
      }
      if (CloseNow)
      {
       if (Socket.Init)
       Socket.Close();
       return(1);
      }
    }// while(1)
    return(1);
   }// if( argc == 3 ) ends

  printf("To send a file use:\n");
  printf("    <program> ip -s filename\n");
  printf("To recieve a file use:\n");
  printf("    <program> ip filename\n");
  MessageBox(0,"Ends","Hello.exe",MB_OK);// ua1zcl added - must be removed
  return(0);
}
//////////////////////////////////////////////////////////////////////////////
0
 

Author Comment

by:krydea
Comment Utility
k,
i have a delphi client and a c server and i have to use this in the delphi client to but there is a problem it does not work if i rewrite a part in delphi <- the problem is not that i can't compile it the problem is that i does not work corectly and i can't do enny thing to it. if i can put it in a dll and put it in he resouce file and use it then.. so that's what i what to do.
0
 

Author Comment

by:krydea
Comment Utility
ps: youcan push reply or something but you can see my email when i send you a email. true or not?
0
 
LVL 4

Expert Comment

by:prashant_n_mhatre
Comment Utility
I'm very busy right now...I'll get back to you....
0
 

Author Comment

by:krydea
Comment Utility
so you what to try to add it to a dll?
0
 

Author Comment

by:krydea
Comment Utility
i tryed something but does not work when i load send i get unknow error..:(

#include <process.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#include <acssvcc.h>

long sum1();
extern "C" int APIENTRY sum2(int i);
extern "C" long APIENTRY sum3(int i,int j);

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

void WaitForConnect(void* pParam);
void WaitForReceive(void* pParam);
char* ArgFile=NULL;
bool WillSnd=false;
bool InitReady=false;
bool CloseNow=false;

class CSocket
{
public:
   CSocket()
   {
      Handle=INVALID_SOCKET;
      Client=NULL;
      Init=Server=Active=FALSE;
   };
   ~CSocket()
   {
   };

   BOOL Open(int af = AF_INET, int type = SOCK_STREAM, int protocol = 0)
   {
      if( !Init )
      {
         {
            WSADATA WSAData;
            if( WSAStartup(MAKEWORD(1,1), &WSAData) != 0 )
               return(FALSE);
         }
         Init=TRUE;
      }
      Handle=socket(af, type, protocol);
      if( Handle == INVALID_SOCKET )
         return(FALSE);
      return(TRUE);
   }

   BOOL Initialize(char* Address, WORD Port)
   {
      HOSTENT hAddress;
      SOCKADDR_IN hSockAddr;
      if( !FillAddress(Address, &hAddress) )
         return(FALSE);
      memcpy((char FAR *)&(hSockAddr.sin_addr), hAddress.h_addr, hAddress.h_length);
      hSockAddr.sin_family=AF_INET;
      hSockAddr.sin_port=Port;
      if( bind(Handle, (struct sockaddr FAR *)&hSockAddr, sizeof(hSockAddr)) == SOCKET_ERROR )
         return(FALSE);
      return(TRUE);
   }
   BOOL StartServer(char* Addr, WORD Port)
   {
      if( !Initialize(Addr, Port) )
         return(FALSE);
      if( listen(Handle, 1) == SOCKET_ERROR )
         return(FALSE);
      Server=Active=TRUE;
      WaitThread=_beginthread(WaitForConnect, 0, (void* )this);
      return(TRUE);
   }
   BOOL StartClient(char* Addr, WORD Port)
   {
      HOSTENT hAddress;
      SOCKADDR_IN hSockAddr;
      if( !FillAddress(Addr, &hAddress) )
         return(FALSE);
      memcpy((char FAR *)&(hSockAddr.sin_addr), hAddress.h_addr, hAddress.h_length);
      hSockAddr.sin_family=AF_INET;
      hSockAddr.sin_port=Port;
      if( connect(Handle, (struct sockaddr FAR *)&hSockAddr, sizeof(hSockAddr)) == SOCKET_ERROR )
         return(FALSE);
      WaitThread=_beginthread(WaitForReceive, 0, (void* )this);
      return(TRUE);
   }
   BOOL Close(void)
   {
      if( Client )
         closesocket(Client);
      if( Handle )
         closesocket(Handle);
      Client=Handle=0;
      Init=Active=FALSE;
      return(TRUE);
   }
   BOOL SendText(char* Text)
   {
      if( send(Handle, Text, strlen(Text)+1, MSG_DONTROUTE) != SOCKET_ERROR )
         return(FALSE);
      return(TRUE);
   }
   BOOL SendFile(char* Text)
   {
      FILE* File;

        //printf("\nSend: %s\n", Text);
      File=fopen(Text, "rb");
      if( !File )
         return(FALSE);
      char Buffer[1024]="@";
      send(Handle, Buffer, 1, MSG_DONTROUTE);
      fseek(File, 0, SEEK_END);
      long Size=ftell(File);
      fseek(File, 0, SEEK_SET);
      //sprintf(Buffer, "%10.10ld", Size);
      send(Handle, Buffer, 11, MSG_DONTROUTE);
      int Len;
      while( (Len=fread(Buffer, 1, 1024, File)) > 0 )
         send(Handle, Buffer, Len, MSG_DONTROUTE);
      fclose(File);
      return(TRUE);
   }
   BOOL FillAddress(char* Address, HOSTENT* hAddress)
   {
      HOSTENT* p=gethostbyname(Address);
      if( !p ) return(FALSE);
      memcpy(hAddress, p, sizeof(HOSTENT));
      return(TRUE);
   }
   char* ErrorFunction(char* Title = NULL)
   {
      int Error=WSAGetLastError();
      char* Msg;
      switch( Error )
      {
         case WSANOTINITIALISED:    Msg="A successful AfxSocketInit must occurbefore using this API."; break;
         case WSAENETDOWN      :    Msg="The Windows Sockets implementationdetected that the network subsystem failed."; break;
         case WSAEADDRINUSE    :    Msg="The specified address is already inuse."; break;
         case WSAEINPROGRESS   :    Msg="A blocking Windows Sockets call is inprogress."; break;
         case WSAEADDRNOTAVAIL :    Msg="The specified address is not availablefrom the local machine."; break;
         case WSAEAFNOSUPPORT  :    Msg="Addresses in the specified familycannot be used with this socket."; break;
         case WSAECONNREFUSED  :    Msg="The attempt to connect was rejected.";break;
         case WSAEDESTADDRREQ  :    Msg="A destination address is required.";break;
         case WSAEFAULT        :    Msg="The nSockAddrLen argument isincorrect."; break;
         case WSAEINVAL        :    Msg="The socket is not already bound to anaddress."; break;
         case WSAEISCONN       :    Msg="The socket is already connected.";break;
         case WSAEMFILE        :    Msg="No more file descriptors areavailable."; break;
         case WSAENETUNREACH   :    Msg="The network cannot be reached from thishost at this time."; break;
         case WSAENOBUFS       :    Msg="No buffer space is available. Thesocket cannot be connected."; break;
         case WSAENOTSOCK      :    Msg="The descriptor is not a socket.";break;
         case WSAETIMEDOUT     :    Msg="Attempt to connect timed out withoutestablishing a connection."; break;
         case WSAEWOULDBLOCK   :    Msg="The socket is marked as nonblocking andthe connection cannot be completed immediately."; break;
         default               :    Msg="Unknown error."; break;
      }
      //if( Title )
         //printf("\r\n%s\r\n%s\r\n", Title, Msg);
      //else
         //printf("\r\n%s\r\n", Msg);
        return(Msg);
      CloseNow=true;
   }

   BOOL Init;
   BOOL Server;
   BOOL Active;
   SOCKET Handle;
   SOCKET Client;
   unsigned long WaitThread;
};

void WaitForConnect(void* pParam)
{
   CSocket* Socket=(CSocket* )pParam;

   Socket->Client=accept(Socket->Handle, NULL, NULL);
   if( Socket->Client != INVALID_SOCKET )
   {
      //printf("\r\nConnected\r\n");
      Socket->Server=FALSE;
      closesocket(Socket->Handle);
      Socket->Handle=Socket->Client;
      Socket->Client=0;
        Socket->WaitThread=_beginthread(WaitForReceive, 0, (void* )Socket);
   }
   else
   {
      //printf("\r\nNot Connected\r\n");
      Socket->Client=0;
      Socket->WaitThread=NULL;
   }
  _endthread();
}

void WaitForReceive(void* pParam)
{
   CSocket* Socket=(CSocket* )pParam;
      
   if (!WillSnd)
   {
         InitReady=true;
   }

   int Ret;
   char Buffer;
   while( (Ret=recv(Socket->Handle, &Buffer, 1, 0) != SOCKET_ERROR) && Ret )
   {
      if( Buffer == '@' ) // File
      {
             //printf("\nRecieve: %s\n", ArgFile);
         FILE* File;
         long nSize, S;
         char Size[11];
         recv(Socket->Handle, Size, 11, 0);
         S=nSize=atol(Size);
         //printf("File: %-10.10ld\r\n", nSize);
         //printf("%-10.10ld", 0l);
         File=fopen(ArgFile, "wb");
         while( nSize)
         {
            static char Buffer[1024]="";
            long BuffSize=1024;
            if( nSize < BuffSize )
               BuffSize=nSize;
            BuffSize=recv(Socket->Handle, Buffer, BuffSize, 0);
            fwrite(Buffer, 1, BuffSize, File);
            nSize-=BuffSize;
            //printf("\b\b\b\b\b\b\b\b\b\b");
            //printf("%-10.10ld", S-nSize);
         }
         fclose(File);
         //printf(" : OK\r\n");
             CloseNow=true;
      }
        else if( Buffer == 'G' ) // Go send
        {
              InitReady=true;
        }
   }
   CloseNow=true;
   //printf("\r\nNot Connected\r\n");
   Socket->WaitThread=NULL;
   _endthread();
}

/*
int main(int argc, char* argv[])
{
   CSocket Socket;
   char* ArgIP=NULL;
   char* ArgSR=NULL;
   //char* ArgFile=NULL;

   if( argc == 4 )
   {
         ArgIP=argv[1];      
         ArgFile=argv[3];
         WillSnd=true;
         //printf("You will send the file %s to %s\n", ArgFile, ArgIP);
         if( !Socket.Open() || !Socket.StartServer(ArgIP, 99) )
         {
               Socket.ErrorFunction();
         }
         //else
               //printf("\nPress Ctrl+C to exit\nWait...\n");

         while(1)
         {
               if (InitReady)
               {
                     InitReady=false;
                     printf("Server Init Completed\n");
                     //if (
                     Socket.SendFile(ArgFile)//)
                     //{
                           //printf("DONE\n");
                     //}
                     //else
                     //{
                           //printf("ERROR\n");
                     //}
               }
               if (CloseNow)
               {
                     if (Socket.Init)
                           Socket.Close();
                     return(1);
               }
         }
         return(1);
   }
   if( argc == 3 )
   {
         ArgIP=argv[1];      
         ArgFile=argv[2];
         WillSnd=false;
         //printf("You will recieve a file from %s and will save it as %s\n", ArgIP, ArgFile);  
         if( !Socket.Open() || !Socket.StartClient(ArgIP, 99) )
         {
               Socket.ErrorFunction();
         }
         else
               //printf("\r\nConnected\r\nPress Q<enter> to exit\n");

         while(1)
         {
               if (InitReady)
               {
                     InitReady=false;
                     //printf("Client Init Completed\n");
                     Socket.SendText("G");
               }
               if (CloseNow)
               {
                     if (Socket.Init)
                           Socket.Close();
                     return(1);
               }
         }
         return(1);
   }
   //printf("To send a file use:\n");
   //printf("    <program> ip -s filename\n");
   //printf("To recieve a file use:\n");
   //printf("    <program> ip filename\n");
   return(0);
}
*/

extern "C" char* APIENTRY SockRecieveFile(char* ChrFile, char* ChrIP, int IntPort)
{
   CSocket Socket;
   char* ArgIP=ChrIP;

   ArgFile = ChrFile;

   WillSnd=false;
   //printf("You will recieve a file from %s and will save it as %s\n", ArgIP, ArgFile);  
   if( !Socket.Open() || !Socket.StartClient(ArgIP, IntPort) )
   {
         return("Cannot connect");
         //Socket.ErrorFunction();
   }
   //else
   //    printf("\r\nConnected\r\nPress Q<enter> to exit\n");

   while(1)
   {
         if (InitReady)
         {
               InitReady=false;
               //printf("Client Init Completed\n");
               Socket.SendText("G");
         }
         if (CloseNow)
         {
               if (Socket.Init)
                     Socket.Close();
               return("DONE");
         }
   }
   return("DONE");
}

extern "C" char* APIENTRY SockSendFile(char* ChrFile, char* ChrIP, int IntPort)
{
   CSocket Socket;
   char* ArgIP=ChrIP;
   char* ErrMsg=NULL;

   ArgFile = ChrFile;

   WillSnd=true;
   //printf("You will send the file %s to %s\n", ArgFile, ArgIP);
   if( !Socket.Open() || !Socket.StartServer(ArgIP, IntPort) )
   {
         //return("ERROR");
         ErrMsg = Socket.ErrorFunction();
         return(ErrMsg);
         //Socket.ErrorFunction();
   }
   //else
         //printf("\nPress Ctrl+C to exit\nWait...\n");
   
   while(1)
   {
         if (InitReady)
         {
               InitReady=false;
               //printf("Server Init Completed\n");
               if (!Socket.SendFile(ArgFile))
               {
                     //printf("DONE\n");
                     return("ERROR");
               }
               //else
               //{
                     //printf("ERROR\n");
               //}
         }
         if (CloseNow)
         {
               if (Socket.Init)
                     Socket.Close();
               return("DONE");
         }
   }
   return("DONE");
}
0
 

Author Comment

by:krydea
Comment Utility
that's in my dfm filei set it when i add TServerSocket..
do you what that i send you my project?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Expert Comment

by:ua1zcl
Comment Utility
krydea, i am sorry, you not asked me to make dll from
this code. What functions this dll must export main()? If so - this dll is ready now. The problem is to send mainDll.dll and all project (10.3 kB zip'ed size)to you.
I can't tell you all procedure how to do all settings. It was not so easy as i expected, VC6 propose his default mfc support which is not needed. In VC5 it was much better.
At last i decided to send it to my second e-mail box:
Go to www.hotmail.com, enter ua1zcl and 99999999 password.
ua1zcl@hotmail.com
Password is 99999999
Download please file mainDll.zip
You can make your mail box there.

0
 

Author Comment

by:krydea
Comment Utility
thanx did you test you dll?
with a programme or something?
0
 

Author Comment

by:krydea
Comment Utility
case i don't know witch function i have to call or what i know how to load dll's but don't know witch function in this dll.
maino? with what arg's like old times?
plz help plz respone..
0
 
LVL 1

Expert Comment

by:ua1zcl
Comment Utility
Yes, krydea, the arguments to the maino(,) are the same
as in exe programm. Calling convention is stdcall or
WINAPI. Dll Not tested, but i do not changed anything
in text there. Perhaps it will works.
0
 

Author Comment

by:krydea
Comment Utility
can you plz test it case i didn't get it to work:(...
0
 

Author Comment

by:krydea
Comment Utility
//k i tryed this:

// hello,                                                //
//    this is my dll.h                                    //
//               krydea                                          //
//                                     Copyright (c)     //
//////////////////////////////////////////////////////////

//------------------------------------------------------//
#include <stdio.h>
#include <windows.h>
//------------------------------------------------------//

/**********************************/
/*load a dll and run's the        */
/*function with the function name */
/*out the dll                     */
/**********************************/
FARPROC LoadDllFunction(HMODULE     hMyDll,const char *functionn_name)
{
FARPROC myfunction = GetProcAddress(hMyDll, functionn_name);

return myfunction;
}
////////////////////DllTest.c

#include "dll.h"
#include <windows.h>


int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
                          LPSTR lpCmdLine,int nCmdShow)
{
     int i=0;

     HMODULE     hMyDll = LoadLibrary("maindll.dll");

     (LoadDllFunction(hMyDll,"maino"))("127.0.0.1 -s c:\autoexec.bat");

FreeLibrary(hMyDll);
return 0;
}


i can't get it to work it normaly works with Delphi dll's...

plz look ad it..
0
 
LVL 1

Expert Comment

by:ua1zcl
Comment Utility
Please wait, must do code. It is no quick task. I go to job now.
0
 

Author Comment

by:krydea
Comment Utility
ok, i will wait. sat plz when your ready....

btw: i you think you can answer this question you can lock it.
0
 
LVL 1

Accepted Solution

by:
ua1zcl earned 220 total points
Comment Utility
krydea , i allocated on ua1zcl@hotmail.com (pwd=99999999) answer consisting of two zip files.
(my daughter's "nataklm" outgoing address was used by mistake)
It loads, maybe can work. Inet address 127.0.0.1 must be
sent by another way. There it is corrected now.
I tryed make  int maino(int,char*[]) as stdcall, but silly
microstuff compiler not want do it.

typedef int _stdcall (*DLLFUNC)(int,char*[]);
DLLFUNC _maino;
 gives an error, but Borland compiler does correctly.
Then maino is _cdecl call (attention!, it is not the same as _stdcall or WINAPI) and expotred by name "_maino".


0
 

Author Comment

by:krydea
Comment Utility
so can i make the arg ("127.0.0.1 -s autoexec.bat") or how must i do that?

plz respone..
0
 

Author Comment

by:krydea
Comment Utility
sorry i mis click on your mail for or of Alex.. i did not read it my apolodize<- i don't know how to spel it i'm dutch and 14 years old...:(
0
 

Author Comment

by:krydea
Comment Utility
k i understand i got it thanx and it works:):)
i will give you the point'snow howevvr i got a small question that you maybe can answer how do i load this dll in delphi

just seard for the function name maino? or don't you know?
 
0
 

Author Comment

by:krydea
Comment Utility
<- btw: i use borland delphi 5.0
0
 
LVL 1

Expert Comment

by:ua1zcl
Comment Utility
I tested this project and (becuse no server on my computer) got error: "Authoritative Answer Host not found."
How to load dll from Delphi - i don't know. I beleave it's
posible, read your help doc's. Must be the same way as
in VC.
Maybe better to use implicit linking, when not necessary to call LoadLibrary() and GetProcAddress(). This way must be also available in Delphi, and not need any changes to dll.
There in project are some MessageBox'es (or Mbox()) you
may remove it.
0
 
LVL 1

Expert Comment

by:ua1zcl
Comment Utility
>>just seard for the function name maino? or don't you know?
Not understand the question. If you wish to rename the
function - not a problem. In mainDll.def file change
_maino to anythig you like.

EXPORTS
;_maino = maino
ConnectToServer = maino

Save file and rebuild.
And now ConnectToServer is a new name of maino.



 
0
 
LVL 1

Expert Comment

by:ua1zcl
Comment Utility
Open from Explorer mainDll.dll by QuckView and in Export
Table you may find exported names of functions. Sometimes no export table presents there, because Microsoft changes standards in any way.
0
 

Author Comment

by:krydea
Comment Utility
cool,
thanx for your help.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now