初学者提问: C++ Builder6.0中如何引用自定义类??????
mesql 2004-04-01 12:08:08 我自定义了一个类(和主程序在同一工程中),然后在主程序中引用它时老是出错,怎么解决啊???我的引用方式有问题吗???
下面是我的代码:
1.自定义类头文件
------------------------------------------------------------------------
//---------------------------------------------------------------------------
#ifndef FTPServiceH
#define FTPServiceH
//---------------------------------------------------------------------------
#include <wininet.h>
//---------------------------------------------------------------------------
class TMyFtpClient: public TObject
{
private:
DWORD FContext;
HINTERNET FINet;
HINTERNET FFtpHandle;
public:
AnsiString FServer;
INTERNET_PORT nServerPort;
bool FConnceted;
protected:
__fastcall ~TMyFtpClient(void);
public:
__fastcall TMyFtpClient(void);
bool __fastcall Connect(void);
};
extern TMyFtpClient *MyFtpClient;
#endif
--------------------------------------------------------------------------
2.自定义类CPP文件
-------------------------------------------------------------------------
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "FTPService.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//----------------------------------------------------------------------------
TMyFtpClient *MyFtpClient;
__fastcall TMyFtpClient::TMyFtpClient(void)
{
FINet=InternetOpen("MyFTPClient",0,NULL,0,0);
FFtpHandle=NULL;
FConnceted=false;
}
__fastcall TMyFtpClient::~TMyFtpClient(void)
{
if(FINet!=NULL)
{
InternetCloseHandle(FINet);
}
if(FFtpHandle!=NULL)
{
InternetCloseHandle(FFtpHandle);
}
}
bool __fastcall TMyFtpClient::Connect(void)
{
AnsiString ErrorMsg;
AnsiString CR1("\x00D\x00A");
FContext=255;
FFtpHandle=InternetConnect(FINet,FServer.c_str(),nServerPort,NULL,NULL,INTERNET_SERVICE_FTP,0,FContext);
if(FFtpHandle==NULL)
{
ErrorMsg="Connection failed"+CR1+"Server:"+FServer;
throw Exception(ErrorMsg);
}
else
FConnceted=true;
return true;
}
--------------------------------------------------------------------------
3.主程序头文件:
--------------------------------------------------------------------------
//---------------------------------------------------------------------------
#ifndef FTPClientServiceH
#define FTPClientServiceH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
#include <wininet.h>
//---------------------------------------------------------------------------
class TFTPClient : public TForm
{
__published: // IDE-managed Components
TButton *btnConnect;
void __fastcall btnConnectClick(TObject *Sender);
private: // User declarations
public: // User declarations
//HINTERNET FINet;
__fastcall TFTPClient(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TFTPClient *FTPClient;
//---------------------------------------------------------------------------
#endif
--------------------------------------------------------------------------
4.主程序CPP文件:
--------------------------------------------------------------------------
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "FTPClientService.h"
#include "FTPService.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFTPClient *FTPClient;
//---------------------------------------------------------------------------
__fastcall TFTPClient::TFTPClient(TComponent* Owner)
: TForm(Owner)
{
// FINet=InternetOpen("FTPClient",0,NULL,0,0);
}
//---------------------------------------------------------------------------
void __fastcall TFTPClient::btnConnectClick(TObject *Sender)
{
MyFtpClient->FServer="192.168.39.139";
MyFtpClient->nServerPort=61129;
MyFtpClient->Connect();
if(MyFtpClient->FConnceted)
ShowMessage("Conncetion succeeded!");
}
//---------------------------------------------------------------------------
然后我编译是没有问题的,但在调试时打开编译后的EXE文件时,老是报错如下:
Access violation at address 400068A4 in module 'rtl60.bpl'.Write of address 00000010.
这是为什么啊???是我引用自定义类的方法有错误吗?????
请各位高手指教啊!!!!!!!!