Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,

Am working creating MFC DLL & am getting an error:Unhandled exception at 0x00000000 in Key_Testing.exe: 0xC0000005: Access violation.

The DLL Code is like this:
C#
//Demo.h
char*	GetDiskSerialNumber (char g_DiskLetter);

//Demo.cpp
char* DLLclass::GetDiskSerialNumber(char g_DiskLetter)
{
	CHAR SN[64];
	memset(SN,0,sizeof(SN));
		
	if(GetSerialNumber(SN,g_DiskLetter)){   // Error in this Line..
		return (char*)SN;
	}
}

This DLL am using in an application..
void CKey_TestingDlg::OnBnClickedgetserial()
{
	// TODO: Add your control notification handler code here
	DLLclass Dllobj;
	char ltr='h';
	char* ser=Dllobj.GetDiskSerialNumber(ltr);
}


Please,, Could anyone suggest where i gone wrong.. Any help will be appriciated..

Thank you.
Posted
Updated 24-Jan-12 18:55pm
v2
Comments
Lakamraju Raghuram 25-Jan-12 1:20am    
Can you give info about GetSerialNumber(...) method you are using
Guru_C++ 25-Jan-12 2:05am    
Thanks for your help :) Function Declaration for GetSerialNumber is like this:
typedef BOOL (WINAPI *GetSerialNumberDLL)(CHAR* , CHAR );
Mohibur Rashid 25-Jan-12 2:31am    
boolian pointer? are you sure?
Guru_C++ 25-Jan-12 2:17am    
I changed my code as per as ur suggestion.. Still a getting same error..:(
Guru_C++ 25-Jan-12 3:08am    
Yes John.. Its boolian pointer..

What is the code inside GetSerialNumber?
There is probably some error there.

Also, you're declaring a local array and returning its address, which will no longer be valid when the function returns.

Instead change it as follows -
void DLLclass::GetDiskSerialNumber(char g_DiskLetter, char* SN)
{
	GetSerialNumber(SN, g_DiskLetter);
}
 
void CKey_TestingDlg::OnBnClickedgetserial()
{
	// TODO: Add your control notification handler code here
	DLLclass Dllobj;
	char ltr='h';

	CHAR SN[64];
	memset(SN,0,sizeof(SN));

	Dllobj.GetDiskSerialNumber(ltr, SN);
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-Jan-12 1:25am    
Aha, good catch. My 5.
--SA
GetDiskSerialNumber is returning a pointer to a local array. This is not valid. Local variables die when the function exits as they reside on the stack.
 
Share this answer
 
Comments
Guru_C++ 25-Jan-12 7:54am    
Yes ur correct.. And, Also am derefrencing a null pointer. But, am not getting how to modify the code..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900