We help IT Professionals succeed at work.

problem dll verry easy..

krydea
krydea asked
on
hello,
mfc dll trying to show a dialog when called but i don't know what to call case i'm not verry good in mfc and it was furst made without.. and then i could call _maino -> main
plz help.. i don't got more point's but plz help me as a E.E member i'm 15 and it's just for fun so plz don't ask anny money..
here is the source file:

// krydea's task he posted me:

#include "stdafx.h"

// hello.cpp
///#define _MT // multithread for process.h
#include <windows.h>
#include <process.h>
#include <winsock2.h>
//#include <windows.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include "ProgressDialog.h"
#include <acssvcc.h>
//#include "myerror.h"


static int iProcAttaches;

 INT  WINAPI DllEntryPoint
(
    HINSTANCE hInst,
    ULONG ul_reason_being_called,
    LPVOID lpReserved
)
{
    switch (ul_reason_being_called)
    {
        case DLL_PROCESS_ATTACH:iProcAttaches++; break;
        case DLL_THREAD_ATTACH:break;
        case DLL_THREAD_DETACH:break;
        case DLL_PROCESS_DETACH: break;
    }
UNREFERENCED_PARAMETER(lpReserved);
return 1;
}
/////////////////////////////////////////////////////////////

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


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 )
      {
            //Mbox(ErrorFunction()); return(FALSE);
      }
    memcpy(hAddress, p, sizeof(HOSTENT));
    return(TRUE);
   }

   PSTR 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;
       case WSAHOST_NOT_FOUND      :Msg="Authoritative Answer Host not found.";
       break;
       case WSATRY_AGAIN:Msg="Non-Authoritative Host not found, or server failure.";
       break;
        case WSANO_RECOVERY      :Msg="Nonrecoverable error occurred.";
       break;
        case WSANO_DATA      :Msg="Valid name, no data record of requested type.";
       break;
        case WSAEINTR      :Msg="The (blocking) call was canceled through ";
       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;
   return Msg;
  }//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 maino(int argc, char* argv[])
{
 ProgressDialog dlg;
 dlg.Create(IDD_DIALOG1);
// dlg.m_status= "Status: Not Connected";
 CSocket Socket;
 char* ArgIP=NULL;
 char* ArgSR=NULL;
 
    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) )
    {
      //Mbox(Socket.ErrorFunction());
        
    }
    else //printf("\r\nConnected\r\nPress Q<enter> to exit\n");
      {//sprintf(str,"\r\nConnected\r\nPress Q<enter> to exit\n");
      //Mbox(str);
            //dlg.m_status= "Status: Connected";
      //            return 1;
      }
 
    while(1)
    {                  
      if (InitReady)
      {
       InitReady=false;
       //Mbox("Client Init Completed\n");
       Socket.SendText("G");
      }
      if (CloseNow)
      {
       if (Socket.Init)
       Socket.Close();
       return(1);
      }
    }// while(1)
    return(1);
   }// if( argc == 3 ) ends

  return(0);
}

if you whant the project plz ask i will mail it or upload it some where..
plz help.

Carlos Smith

Comment
Watch Question

Author

Commented:
btw: i'm trying to load the dll in delphi..
CERTIFIED EXPERT
Author of the Year 2009

Commented:
krydea,
I think that when you titled this ...

    problem dll verry easy...

...you were attempting to deceive us!  Shame on you!
I think I understand.
You originally created this code using the standard Windows API and now you want to add some MFC code to it.

You're asking what is the function you call to actually launch the code (similar to main()).

My suggestion to you is that you start over completely.
Make your MFC AppWizard EXE from the beginning and add your old code once you figure out where it needs to go.

If you try anything else without a thorough understanding of the framework, you're asking for a ton of headaches.

The path you're on will take an extreme amount of time just to get to a functioning application.

So here's your answer:
   Start over using the wizard.
...or MFC AppWizard DLL

Author

Commented:
when i got more point's !! can you explaine more or maybe give a example?

Author

Commented:
DanRollins:
maybe it was something simple i don't know how to do it so!!
and if i get more point's sure i will give it on someone for the question!
File -> New -> [Projects]

Choose "MFC AppWizard (dll)"

Give your project a name.
Click OK

Choose "Regular DLL with MFC statically linked"
Click "Finish"
Click "OK"
----------------Generic Steps-----------------
Set your project for either Release or Debug mode.
Compile

Once you have zero warnings and zero errors, you are ready to begin.  You will NOT have to call a main() type of function, the framework will do it for you.  But you're making a DLL, so it doesn't matter anyway.

Author

Commented:
thanx can you btw: say what the function is in the uper source case i have to inalize the function!<- just trying.
...and by the way, do not name any of your classes after existing classes.
You are creating a class called CSocket.
Maybe you want it to inherit from CSocket?

If so, do something like
class MySocket : public CSocket
{
   //stuff goes here
};
You don't need to initialize functions in DLLs.

Author

Commented:
can you fix the dll for me? plz..

Author

Commented:
you inalize a dll and then you can call a function in a dll true?

Author

Commented:
...?
No.
You make the DLL and store it.
The program using the DLL will call the functions.
There is not really any initialization that you need to worry about.

Later, when you're making classes, you'll initialize things in the class, but worry about that later.

I will email you a test project that you can modify to see how it works.

I even use Excel to call the functions in the DLL.

You may need to change the location of the DECLARE in the spreadsheet code so that it points to wherever you put the DLL.
Oh, be sure to check the contents of krydea.cpp and the DEF file.

Author

Commented:
thanx but i don't know how i can do this with the stuff i whant ot put in..how can i call the class?

Author

Commented:
plz help as a E.E member when i get point's i will give you..