c語言控制電腦
1. 用c語言怎樣控制硬體
如果是電腦程序的話,譬如你在vc6.0裡面c語言裡面編寫一個程序,那麼vc6裡面的編譯器會把你的c語言語句按照對應的關系翻譯成你計算機能識別的計算機語句,然後你電腦cpu會按照這些指令來控制你的硬碟,內存讀寫之類的硬體操作。
如果是單片機之類的話,譬如你在keil裡面用c語言編寫了一個單片機程序,那麼keil的編譯器會把你的c語言程序編寫成匯編語言程序文件,然後把那個匯編程序文件燒到單片機晶元上就可以讓晶元按照指令工作了。
2. c語言惡作劇控制電腦命令
關機命令吧 在stdlib頭文件裡面 system("關機指令") 就是shutdown這些 你可以網路system函數
3. 如何用C語言對計算機的硬體進行控制
#include <windows.h>
#include <mmsystem.h>
#pragma comment(lib, "WINMM.LIB")
int main(int argc, char *argv[])
{
mciSendString("Set cdaudio door open wait", NULL, 0, NULL);
return 0;
}
學習一下 windows程序設計 你就知道了。就像樓上說的。windows提供了很多api函數,可以編寫諸如驅動程序、防火牆、游戲等等。
4. c語言控制自己電腦
問的不清不楚..
用按鍵精靈把~
5. C語言遠程式控制制人家電腦,也就是輸入一些命令,怎麼做到
無法控制,首先現在的防火牆可以阻止大多數攻擊,你連人家的IP都連接不上如何談起攻擊啊?其次你不知道他的IP地址,一般撥號上網或是光纖介面的IP地址都是隨機分配的 很不固定。所以說無法用C語言攻擊指定電腦。除非用黑客軟體,成功的種下木馬!
6. 用C語言寫電腦的DOS控制台操作系統
cmd命令非常多,寫的話就是命令識別和動作響應兩部分,
命令識別
涉及詞法語法識別,需要編譯技術;
動作響應
調用系統的api即可;
可以用藉助lex和yacc來寫
7. C語言是以什麼為基礎的(英文或......)怎麼讀呢是怎麼"控制"整台電腦的給我詳細講一下,拜託各位大蝦了
C語言是所有編程語言的基礎
你那個怎麼讀?沒聽懂意思
控制整台電腦?
這比較麻煩
是個相當龐大的程序
C語言中的指針是個很強大的東西
因為它是直接跟內存打交道的
你可以寫個死循環
定義無數個指針
很快就可以令電腦死機
8. C語言是如何控制硬體的
1.指針變數p分配在棧上,而字元串"abcdefg"分配在文字常量區。
2.是的。指針也是編譯器生成的數據類型,是個長整型。
3.內存地址分為5塊,堆區,棧區,全局(靜態)數據區(分為未初始化和初始化區),
文字常量區,代碼區。存儲在不同區域的數據釋放時間不同,存儲在堆區的數據必須由程序員負責釋放,用free或者是delete。而棧區的由系統自動釋放。全局和靜態的生存期就是整個程序運行期。
4.首先,概念性錯誤,編譯器並不執行程序,執行程序的是你的系統。編譯器只是實現了高級語言向機器語言的轉化而已。至於計算機的工作原理,這個概念也太泛了,有硬體層也有軟體層。硬體層基本也就是80x86的匯編和內存管理,軟體層也就是操作系統了,請問你想知道什麼?
5.a是數組名,最重要的是a是一個指針常量,你該了解了吧,常量是不能參與指針運算的,所以a++是錯的。關於calloc和malloc,釋放內存方面沒什麼區別(這個是個人觀點)。
關於指針變數沒有初始化的問題。其實指針本身也是個長整行的變數,他存放的地址完全可以理解為4*8的整數,那麼他的分配方式也跟普通的變數一樣了。如果你是全局指針,那麼編譯器會把它自動初始化為00000000(十六進制),如果是局部指針,那麼這個量就是未知的了,根據函數堆棧有不同的結果。null也就是內存被寫成了00000000(十六進制).這個空不是說不存在,而是內存被寫成了4*8=32位的全0.
9. 如何通過c語言來控制電腦硬體,比如網卡,音效卡,攝像頭等
這個需要涉及到嵌入式領域了。簡單一點就是你得懂硬體和軟體,能夠根據datasheet寫出硬體驅動程序
10. 如何用C語言控制計算機串口
基本方法是使用CreateFile來建立一個串口文件,然後用overlap的方式進行讀寫
#define SERAIL_PORT_BUF_MAX (1024*8)
typedef HRESULT (*PFN_CMD_PARSE_DATA)(HANDLE hParseApp, LPCSTR szRspCmd, int nCmdLen);
class CUsbSrvApp// : public CWinApp
{
public:
CUsbSrvApp();
~CUsbSrvApp();
BOOL OnSendData(const char *szBuf, int nLen);// 發送數據
int ComConnect(CString strPort); // 連接COM口
HANDLE OpenComPort(CString strPort, int nBaudRate, int nDataBits, int nStopBits, int nParity, int nFlowCtrlType); // 打開串口
void Close(); // 關閉串口
HANDLE m_hCom;
BOOL m_bConnected;
OVERLAPPED m_OverlappedRead;
OVERLAPPED m_OverlappedWrite;
CWinThread *m_pThread;
PFN_CMD_PARSE_DATA m_pRspCmdFunc; // 用來處理接受數據的CALLBACK
HANDLE m_hParseApp;
};
CUsbSrvApp::CUsbSrvApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
m_bConnected = false;
m_hCom = NULL;
m_pRspCmdFunc = NULL;
}
CUsbSrvApp::~CUsbSrvApp()
{
}
//打開串口通信,並返回串口句柄
HANDLE CUsbSrvApp::OpenComPort(CString strPortName,
int nBaudRate,
int nDataBits,
int nStopBits,
int nParity,
int nFlowCtrlType)
{
DCB dcb;
COMMTIMEOUTS CommTimeOuts ;
COMMCONFIG ComConfig;
HANDLE hComPort;
CString strPort;
strPort.Format("\\\\.\\%s",strPortName); // COM口的文件名應該是 \\.\COMXX
//打開窗口其實就是創建一個文件
hComPort = CreateFile(strPort,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,
NULL);
if (INVALID_HANDLE_VALUE == hComPort)
return INVALID_HANDLE_VALUE;
// 設置一些COM口通訊參數和OVERLAP
CommTimeOuts.ReadIntervalTimeout = -1;
CommTimeOuts.ReadTotalTimeoutConstant = 0;
CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
CommTimeOuts.WriteTotalTimeoutConstant = 0;
CommTimeOuts.WriteTotalTimeoutMultiplier = 0x1388;
SetCommTimeouts( m_hCom, &CommTimeOuts ) ;
SetDefaultCommConfig(strPortName, &ComConfig, sizeof(COMMCONFIG));
GetCommState(m_hCom, &dcb ) ;
dcb.BaudRate = nBaudRate;
dcb.ByteSize = nDataBits;
dcb.StopBits = nStopBits;
dcb.fParity = (NOPARITY != nParity);
dcb.Parity = nParity;
//set the receive char
dcb.EvtChar = 0x0D;
switch(nFlowCtrlType)
{
case 0: //no flow control
break;
case 1://HARD_FLOW_CTRL:
dcb.fOutxCtsFlow = TRUE;
dcb.fOutxDsrFlow = TRUE;
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.fDsrSensitivity = TRUE;
dcb.fRtsControl = RTS_CONTROL_TOGGLE;
break;
case 2://SOFT_FLOW_CTRL:
dcb.fOutX = TRUE;
dcb.fInX = TRUE;
break;
}
BuildCommDCB(_T("baud=115200 parity=N data=8 stop=1"),&dcb);
SetCommState(hComPort, &dcb ) ;
SetCommMask(hComPort, 0);
SetCommMask(hComPort, EV_RXCHAR|EV_CTS|EV_DSR|EV_RLSD|EV_RING);
SetupComm( hComPort, SERAIL_PORT_BUF_MAX,SERAIL_PORT_BUF_MAX) ;
//clear read and write buffer
PurgeComm( hComPort, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR );
return hComPort;
}
void CUsbSrvApp::Close()
{
if(m_bConnected)
{
m_bConnected = false;
CloseHandle(m_hCom);
m_hCom = NULL;
}
}
// 這個線程是監視串口數據,一旦有數據則讀取並調用CALLBACK通知客戶端
UINT ReceiveComData(LPVOID pParam)
{
CUsbSrvApp *pUsbSrv = (CUsbSrvApp *)pParam;
HANDLE hComPort = pUsbSrv->m_hCom;
DWORD dwEvtMask=0;
DWORD dwErrorFlags;
SetCommMask( hComPort, EV_RXCHAR);
OVERLAPPED osRead;
osRead.hEvent = CreateEvent(NULL,FALSE,FALSE,NULL);
DWORD dwTransfer = 0;
while(pUsbSrv->m_bConnected)
{
if( !WaitCommEvent( hComPort, &dwEvtMask,&osRead))
{
if( GetLastError()== ERROR_IO_PENDING)
{
WaitForSingleObject(osRead.hEvent, INFINITE);
if(dwEvtMask&EV_RXCHAR==EV_RXCHAR)
{
COMSTAT ComStat={0} ;
DWORD dwReadLen = 0;
DWORD dwBytesRead = 0;
DWORD dwTotalLen = 0;
ClearCommError(hComPort, &dwErrorFlags, &ComStat );
dwTotalLen = ComStat.cbInQue;
dwReadLen = (SERAIL_PORT_BUF_MAX > dwTotalLen)?dwTotalLen:SERAIL_PORT_BUF_MAX;
BYTE *pBuf = new BYTE[dwTotalLen+1];
memset(pBuf, 0 , dwTotalLen+1);
DWORD nReadBufLen=0;
while(dwTotalLen>0)
{
if(FALSE == ReadFile( hComPort, pBuf+nReadBufLen,dwReadLen, &dwBytesRead,&pUsbSrv->m_OverlappedRead))
{
if(GetLastError() == ERROR_IO_PENDING)
{
GetOverlappedResult(hComPort,&osRead, &dwTransfer, TRUE );
}
break;
}
nReadBufLen +=dwBytesRead;
dwTotalLen -=dwBytesRead;
dwReadLen -= dwBytesRead;
dwReadLen = (SERAIL_PORT_BUF_MAX>dwReadLen)?dwReadLen:SERAIL_PORT_BUF_MAX;
}
if(pUsbSrv->m_pRspCmdFunc!=NULL&&nReadBufLen!=0)
{
pUsbSrv->m_pRspCmdFunc(pUsbSrv->m_hParseApp, (char*)pBuf,nReadBufLen);
}
delete pBuf;
ClearCommError(hComPort, &dwErrorFlags, &ComStat );
int len =0;//= m_retList.GetSize();
}//endif if(dwEvtMask&EV_RXCHAR==EV_RXCHAR)
}//endif if( GetLastError()== ERROR_IO_PENDING)
}//endif if( !WaitCommEvent( hComPort, &dwEvtMask,&o))
else
{
if(GetLastError() == ERROR_IO_PENDING) {
GetOverlappedResult(hComPort, &osRead, &dwTransfer, TRUE ); // sleep thread
}
}
Sleep(1);
} //endwhile while(m_bConnected)
return 0;
}
int CUsbSrvApp::ComConnect(CString strPort)
{
int nBaudRate = 115200;
int nDataBits = 8;
int nStopBits = 1;
int nParity = 0;
int nFlowCtrl = 1;
if (NULL != m_hCom || m_bConnected)
{
return 0;
}
m_hCom = OpenComPort(strPort,nBaudRate,nDataBits,nStopBits,nParity,nFlowCtrl);
if( INVALID_HANDLE_VALUE == m_hCom)
{
m_hCom = NULL;
return 0;
}
memset( &m_OverlappedRead, 0, sizeof( OVERLAPPED ) );
memset( &m_OverlappedWrite, 0, sizeof( OVERLAPPED ) );
m_OverlappedRead.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
m_OverlappedWrite.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
m_pThread = AfxBeginThread( ReceiveComData,(void*)this,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED ,NULL );
if( NULL == m_pThread )
{
CloseHandle( m_hCom );
m_hCom = NULL;
return FALSE;
}
else
{
m_bConnected = TRUE;
m_pThread->ResumeThread( );
}
return TRUE;
}
int CUsbSrvApp::OnSendData(const char *szBuf, int nLen)
{
BOOL bWriteStat;
BOOL bWrite = TRUE;
DWORD dwBytesWrite = 0;
DWORD dwBytesWritten = 0;
int dwByteswrittenTotal = 0;
if (NULL == m_hCom)
return 0;
int nSentTimes=0;
while(dwByteswrittenTotal<nLen&&nSentTimes<10)
{
nSentTimes++;
dwBytesWrite = nLen-dwByteswrittenTotal;
bWriteStat = WriteFile( m_hCom, szBuf+dwByteswrittenTotal, dwBytesWrite, &dwBytesWritten, &m_OverlappedWrite );
if( !bWriteStat)
{
if ( GetLastError() == ERROR_IO_PENDING )
{
dwBytesWritten = 0;
bWrite = FALSE;
}
}
if (!bWrite)
{
bWrite = TRUE;
bWriteStat = GetOverlappedResult(m_hCom, // Handle to COMM port
&m_OverlappedWrite, // Overlapped structure
&dwBytesWritten, // Stores number of bytes sent
TRUE); // Wait flag
//deal with the error code
}
dwByteswrittenTotal += dwBytesWritten;
}
if(dwByteswrittenTotal<nLen)
return 0;
else
return 1;
}