October 11, 2015, 2:05 pm
The program worked correctly when the numbers were in descending order, but when I changed the array to ascending order it is giving me wrong output.
Code:
#include<iostream>
using namespace std;
int binarySearch(int [], int, int); // function prototype
const int SIZE = 16;
int main()
{
int found, value;
int array[] = { 0,2,2,3,5,9,11,12,12,12,13,17,18,19,19,34 };
// array to be searched
cout << "Enter an integer to search for:" << endl;
cin >> value;
found = binarySearch(array, SIZE, value); //function call to perform the binary search
//on array looking for an occurrence of value
if (found == -1)
cout << "The value " << value << " is not in the list" << endl;
else
{
cout << "The value " << value << " is in position number "
<< found + 1 << " of the list" << endl;
}
system("pause");
return 0;
}
//*******************************************************************
// binarySearch
//
// task: This searches an array for a particular value
// data in: List of values in an orderd array, the number of
// elements in the array, and the value searched for
// in the array
// data returned: Position in the array of the value or -1 if value
// not found
//
//*******************************************************************
int binarySearch(int array[],int numElems,int value) //function heading
{
int first = 0; // First element of list
int last = numElems - 1; // last element of the list
int middle = 0; // variable containing the current
// middle value of the list
while (first <= last)
{
middle = first + (last - first) / 2;
if (array[middle] == value)
return middle; // if value is in the middle, we are done
else if (array[middle]<value)
last = middle - 1; // toss out the second remaining half of
// the array and search the first
else
first = middle + 1; // toss out the first remaining half of
// the array and search the second
}
return -1; // indicates that value is not in the array
}
↧
October 12, 2015, 6:14 am
Hello all.
I am currently working on imaging processing using arrays to store R,G,B values from a 24 bit BITMAP image of width 120 and height 100 pixels.
Visual Studio 2010 is being used.
I have currently extracted the individual R,G,B values into three separate2D arrays from the 24 bit bitmap (it is assumed correct as the correct R,G,B values have been written to a text file with the right pixel count as well).
The task is to take these individual R,G,B values and restore them back into an array (either 1D or 2D), which is then written to an image file. The output should be identical to the original image.
I have tried the following but the output is currently incorrect (same width, height and memory size but colouring is incorrect).
Appreciate your guidance and feedback.
Code:
#include <iostream>
#include <fstream>
#include <windows.h>
#include <WinGDI.h>
unsigned char** Allocate2DArray(int w, int h)
{
unsigned char ** buffer = new unsigned char * [h]; // allocate the rows
unsigned char * memory_pool = new unsigned char [w*h]; // allocate memory pool
for (int i = 0; i < h; ++i)
{
buffer[i] = memory_pool; // point row pointer
memory_pool += w; // go to next row in memory pool
}
return buffer;
}
void DeAllocate2DArray(unsigned char** buffer)
{
delete [] buffer[0]; // delete the memory pool
delete [] buffer; // delete the row pointers
}
using namespace std;
int main()
{
const int width = 120;
const int height = 100;
int bytesPerPixel = 3;
unsigned char m_cHeaderData[54];
unsigned char** m_cImageData = new unsigned char* [height];
for( int i = 0; i <height; i++)
{
m_cImageData[i] = new unsigned char [width*bytesPerPixel];
}
ifstream* m_pInFile;
m_pInFile = new ifstream;
m_pInFile->open("image.bmp", ios::in | ios::binary);
m_pInFile->seekg(0, ios::beg);
m_pInFile->read(reinterpret_cast<char*>(m_cHeaderData), 54);
for(int i = 0; i <height; i++)
{
m_pInFile->read(reinterpret_cast<char*>(m_cImageData[i]), width*bytesPerPixel);
}
m_pInFile->close();
// Declare a pointer of the type you want.
// This will point to the 1D array
unsigned char* array_1D;
array_1D = new unsigned char[height*width*bytesPerPixel];
if(array_1D == NULL) return 0; // return if memory not allocated
// Copy contents from the existing 2D array
int offset = 0;
for(int j=0; j<height; j++) // traverse height (or rows)
{
offset = width * bytesPerPixel* j;
for(int i=0; i<width*bytesPerPixel; i++) // traverse width
{
array_1D[offset + i] = m_cImageData[j][i]; // update value at current (i, j)
}
}
// Declare three 2D arrays to store R,G, and B planes of image.
unsigned char**arrayR_2D, **arrayG_2D, **arrayB_2D;
arrayR_2D = Allocate2DArray(width, height);
arrayG_2D = Allocate2DArray(width, height);
arrayB_2D = Allocate2DArray(width, height);
// return if memory not allocated
if(arrayR_2D == NULL || arrayG_2D == NULL || arrayB_2D == NULL) return 0;
// Extract R,G,B planes from the existing composite 1D array
ofstream RGBdata2D;
RGBdata2D.open("RGBdata2D.txt");
int pixelCount = 0;
int offsetx = 0;
int counter = 0;
for(int j=0; j<height; j++) // traverse height (or rows)
{
offsetx = width * j * bytesPerPixel;
for(int i=0; i<width*bytesPerPixel; i+=bytesPerPixel) // width
{
arrayB_2D[j][counter] = array_1D[offsetx + i+0];
arrayG_2D[j][counter] = array_1D[offsetx + i+1];
arrayR_2D[j][counter] = array_1D[offsetx + i+2];
RGBdata2D<<"B: "<< (int)arrayB_2D[j][counter] << " G: " << (int)arrayG_2D[j][counter] << "R: " << (int)arrayR_2D[j][counter]<< endl;
pixelCount++;
++counter;
}
counter = 0;
}
RGBdata2D<<"count of pixels: "<< pixelCount << endl;
RGBdata2D.close();
//put RGB from 2D array contents back into a 1D array
offset = 0;
counter = 0;
for(int j=0; j<height; j++) // traverse height (or rows)
{
offset = width * bytesPerPixel * j;
for(int i=0; i<width*bytesPerPixel; i+=bytesPerPixel) // width
{
array_1D[offset + i+0] = arrayB_2D[j][counter++];
array_1D[offset + i+1] = arrayG_2D[j][counter++];
array_1D[offset + i+2] = arrayR_2D[j][counter++];
}
counter = 0;
}
ofstream* m_pOutFileRGB;
m_pOutFileRGB = new ofstream;
m_pOutFileRGB->open("imageCopyRGB.bmp", ios::out | ios::trunc | ios::binary);
m_pOutFileRGB->write(reinterpret_cast<char*>(m_cHeaderData), 54); //bitmap bits start at offset 1078
for(int i = 0; i <height; i++)
{
m_pOutFileRGB->write(reinterpret_cast<char*>(array_1D), width*bytesPerPixel);
}
m_pOutFileRGB->close();
// After complete usage, delete the memory dynamically allocated
DeAllocate2DArray(arrayR_2D);
DeAllocate2DArray(arrayG_2D);
DeAllocate2DArray(arrayB_2D);
// After complete usage, delete the memory dynamically allocated
delete[] array_1D; //delete the pointer to pointer
for(int i = 0; i <height; i++)
{
delete[] m_cImageData[i];
}
delete[] m_cImageData;
system("pause");
return 0;
}
↧
↧
October 12, 2015, 10:50 pm
Hello,
I am using Visual Studio Community 2015 to develop a test application that uses a ComboBoxes'. At first I'd like to say that I am a beginner in programming in VC++ and MFC. I have searched multiple sites with no success for an example how to implement it. I am trying to create a Combobox that will show strings (numbers) withing a specific range, ex. 1 to 31. I use a "for" loop to create this range. The "for" loop is defined in "CMFCApplication1Dlg::OnCbnSelchangeCombo2()" function. I have created a basic layout in Dialog Box using Toolbox. The range of numbers are generated correctly upon first pull down request from the ComboBox. Every time I pull down the menu after that another set of 31 numbers is added. I know this correct behavior, but I can't figure out how to stop duplicating on every additional pull down. I have tried to use "UpdateData(FALSE)" command, "if" statements, and it will not stop duplicating the range. This is all happening when I create layout with the resource editor and Toolbox (automatic creation). Also, I have tried an option with manually creating the ComboBox ( in "BOOL CMFCApplication1Dlg::OnInitDialog()" ), with all its parameters, and I had no such issue. All worked as it should, no duplicates. The problem with this approach is that the ComboBox layout is not showing on DialogBox. Please advise what can be done to stop adding the duplicates in automated dialogbox layout. Thank you.
This function "CMFCApplication1Dlg::OnCbnSelchangeCombo2();" is executed in "BOOL CMFCApplication1Dlg::OnInitDialog()".
void CMFCApplication1Dlg::OnCbnSelchangeCombo2()
{
// TODO: Add your control notification handler code here
//UpdateData();
CString str, Out;
for (int i = 1; i <= 31; i++) {
str.Format(_T("%d"), i);
m_handleNumOfDays.AddString(str);
}
// The following is added to see Index of the string.
int idx = m_handleNumOfDays.GetCurSel();
if (idx < 0) return;
m_handleNumOfDays.GetLBText(idx, str);
Out.Format(_T("Index%d for\n%s"), idx, str);
AfxMessageBox(Out);
}
↧
October 14, 2015, 2:05 am
Hi Everyone,
I'm trying to find a way to avoid flickering while drawing graphics.
One method I've used for quite some time was this one, which works great:
http://www.codeproject.com/Articles/...Drawing-In-MFC
The problem is however that it causes some conflicts in VS2013 when I try to "Use MFC in a Static Library".
There's errors like
Code:
error LNK2005: "public: virtual __thiscall CMemDC::~CMemDC(void)" (??1CMemDC@@UAE@XZ) already defined in MyFile.obj
because it uses a custom CMemDC.
Does anyone happen to know a way to use the solution from this website in combination with static libraries?
Alternatively, can anyone provide a different method for double buffering that does not have these kinds of conflicts?
PS:
For what it's worth, I have already found one myself, namely:
Code:
void CMyView::OnDraw(CDC* pDC)
{
CRect rect;
GetClientRect(rect);
CDC cdc;
CBitmap bitmap;
cdc.CreateCompatibleDC(pDC);
bitmap.CreateCompatibleBitmap(pDC, GetScrollPos(SB_HORZ) + rect.Width(), GetScrollPos(SB_VERT)+rect.Height());
CBitmap * oldBitmap = cdc.SelectObject(&bitmap);
CDC * PDC = &cdc;
CODE TO DRAW THE SCREEN WITH PDC
pDC->BitBlt(GetScrollPos(SB_HORZ), GetScrollPos(SB_VERT), rect.Width(), rect.Height(), &cdc, GetScrollPos(SB_HORZ), GetScrollPos(SB_VERT), SRCCOPY);
This solution works reasonably fine, however, it doesn't run entirely smoothly if the scroll bars are used.
This is due to the use of the GetScrollPos(SB_HORZ) parts of the code, which means that in a large field with a small window the bitmap needs to be GetScrollPos(SB_HORZ) + rect.Width() wide and GetScrollPos(SB_VERT)+rect.Height() high, while only the rect.Width(),rect.Height() rectangle is actually drawn in.
↧
October 14, 2015, 2:31 pm
hi, Im using Visual Studio 2015 C++ and trying to include 2 libraries(curl, and secp256k1) installed from nuget, into my project.
but trying to use any functions from the library results in a unresolved external symbol error, which i assume is because
my project is only including the header declarations and not the actual definitions, so how do i get VS to include the declarations into
my project?
↧
↧
October 15, 2015, 12:06 am
Hi,
In my project i'm using dynamic dll. I run my application using Visual Studio 2012, its working good.
But When i was run the exe file with dll file, The application opened successfully, Error Occurs when the Button Click Event Process called. [Button Click Event Process: a function called from dll and save a file information using the same dll function.]
The event called more than one time then getting "Application has stopped working" error.
But the same application and button click event function works good even more then 10 times when run via Visual Studio.
What is the reason?
How can i recover my application from this problem?
↧
October 15, 2015, 12:59 am
Is it possible to change the compiler at run time? What I mean is, in configuration manager, we can add a compiler for 64-bit, but default 32-bit is already there.
Now in the source code, is it possible to put a condition using compile flags (#ifdef <flag>, #endif) that if the source code is compiled in 64-bit OS, then it should automatically use 64-bit compiler and in 32-bit it should use 32-bit compiler?
↧
October 16, 2015, 5:33 am
Hello. I am currently trying to copy an array of ‘double’ values into an array of “unsigned char”.
I have checked my code to know that the expected values in 2 dimensional double arrays, sig_out_imageR, sig_out_imageG and sig_out_imageB are correct (values were written to a file).
I am currently trying to get these values into a 1 dimensional array of unsigned char for later processing. However, the values are incorrect (normally down by 1). For example, instead of the value being 36, it’s 35, or instead of the value being 204, it’s 203.
I have tried casting the double to an unsigned char in hope of preventing loss of data but this clearly hasn’t helped.
I am using Visual Studio 2010. Would appreciate to learn how to fix this error, thank you.
Code:
//put RGB from 2D array contents back into a 1D array
unsigned char* array_1D_RGB_FFT;
array_1D_RGB_FFT = new unsigned char[height*width*bytesPerPixel];
if (array_1D_RGB_FFT == NULL) return 0; // return if memory not allocated
ofstream RGBdata1D;
RGBdata1D.open("RGBdata1D.txt");
int offset = 0;
int counter = 0;
int bytesPerPixel = 3;
for (int j = 0; j<height; j++) // traverse height (or rows)
{
offset = width * bytesPerPixel * j;
for (int i = 0; i<width*bytesPerPixel; i+= bytesPerPixel) // width
{
array_1D_RGB_FFT[offset + i + 0] = static_cast<unsigned char>(sig_out_imageB[j][counter]);
array_1D_RGB_FFT[offset + i + 1] = static_cast<unsigned char>(sig_out_imageG[j][counter]);
array_1D_RGB_FFT[offset + i + 2] = static_cast<unsigned char>(sig_out_imageR[j][counter]);
RGBdata1D << "B: " <<(int)array_1D_RGB_FFT[offset + i + 0]
<< " G: " <<(int)array_1D_RGB_FFT[offset + i + 1]
<< " R: " <<(int)array_1D_RGB_FFT[offset + i + 2]<< endl;
++counter;
}
counter = 0;
}
RGBdata1D << "count of pixels: " <<c<< endl;
RGBdata1D.close();
↧
October 16, 2015, 7:33 am
Hello,
I have a class wizard generated wrapper CWnd class for an ActiveX control. In my project I derived a custom class from that and use this class via Create. I declared an eventsink map to listen for the ActiveX events in dialogs where I actually use the ActiveX. Now I would like to fire the same events from my wrapper (like the built-in ones from the ActiveX control itself) to the dialog so that the same event handler mechanism gets invoked.
What I tried is to get rid of all the event handling in the dialogs and send from the wrapper WM_NOTITY messages to the dialogs instead. The idea was to use the ActiveX like an MFC class like CListCtrl. But my messages are only handled in OnNotify and are then reflected back to the control. Apart from having to change the events in every dialog using the ActiveX it is also not working as expected.
The solution would be to do 'something like COleControl::FireEvent' but from my CWnd wrapper. Is this possible?
Robin
↧
↧
October 17, 2015, 4:08 am
Hi,
I recently switched to VS2013 and for some reason edit+continue (in debugging mode) is no longer working.
In VS2010, whenever I changed code in debugging mode, Alt+F10 would make Visual Studio update the changed code and continue with the program.
This no longer appears to be working in VS2013.
I have Tools->Options->Debugging->Edit and Continue->"Enable Edit and Continue" ON, so that's not the problem here.
Is there possibly some new shortcut for this in VS2013?
I've tried installing VS2013 on two other computers, and it worked in none of them, so it's probably also not something with the installation.
Can anyone tell me what I'm missing?
Thank you in advance!
↧
October 17, 2015, 8:47 am
Can anybody please advise how I can compile and use MFC code in Visual C++ 2010 Express edition?
I know MFC is not available in the Express Edition and I don't have Visual Studio 2010 (Please don't advise me to get it as I don't intend to soon and yes I know about the 1 year trial pack which I am not sure is now offered).
I am not very sure but I believe there was a way to do it in the Express 2008 edition. I don't remember the exact thread. Is it possible to do so using VSE C++ 2010?
Your help will be greatly appreciated.
↧
October 19, 2015, 1:33 am
I read following forum post about the problem that fread() function can't read large files:
http://stackoverflow.com/questions/7...-read-by-fread
I am now writing a program that need large file support on VS2008, I use fopen, fread and fwrite to process files, some of the files maybe about 4G. If there is a bug in fread() function in Visual C runtime library, what is the version of the Visual C++? Thank you!
↧
October 19, 2015, 3:40 am
Hello. I have been testing my Discrete Fourier Transform program for 1 dimensional data (written in using C++).
For the following test input data(X), the corresponding DFT(X) output is expected. The first number in each separate column is the REAL and the second number is the IMAGINARY.
Code:
X DFT X
( 1.000, 0.000) ( 4.000, 0.000)
( 1.000, 0.000) ( 1.000,-2.414)
( 1.000, 0.000) ( 0.000, 0.000)
( 1.000, 0.000) ( 1.000,-0.414)
( 0.000, 0.000) ( 0.000, 0.000)
( 0.000, 0.000) ( 1.000, 0.414)
( 0.000, 0.000) ( 0.000, 0.000)
( 0.000, 0.000) ( 1.000, 2.414)
My program produces the output below for the same input test data.
Code:
(0.5, 0)
(0.125, -0.302)
(-2.3e-017, -2.78e-017)
(0.125, -0.0518)
(0, -3.06e-017)
(0.125, 0.0518)
(4.11e-017, -4.16e-017)
(0.125, 0.302)
The program code is below. I would appreciate to learn where the error lies and how to fix this, thank you!
Code:
bool inverse = false;
int n = 8;
double gRe[8] = {0,1,0,0,0,0,0,0};
double gIm[8] = {0,0,0,0,0,0,0,0};
double GRe[8] = {0,0,0,0,0,0,0,0};
double GIm[8] = {0,0,0,0,0,0,0,0};
for(int w = 0; w < n; w++)
{
GRe[w] = GIm[w] = 0;
for(int x = 0; x < n; x++)
{
double a = -2 * pi * w * x / float(n);
if(inverse) a = -a;
double ca = cos(a);
double sa = sin(a);
GRe[w] += gRe[x] * ca - gIm[x] * sa;
GIm[w] += gRe[x] * sa + gIm[x] * ca;
}
if(!inverse)
{
GRe[w] /= n;
GIm[w] /= n;
}
}
fstream DFTdata;
DFTdata.open("DFT_Bdata.txt");
for (int i = 0; i<n; i++) // width
{
DFTdata <<"Input: "<< testReal[i]
<<" Real: "<< setprecision(3)<<FReal[i] <<" Imag: "<<setprecision(3)<<FIma[i]<< endl;
}
DFTdata.close();
↧
↧
October 21, 2015, 2:49 am
My intention is to setup an scecific file extension on CFileDialog ... For that, I derived a class from CFileDialog, named CFileDialogExt. I have 5 different file types, like this:
Code:
LPTSTR szFilter = _T("Bitmap Files (*.bmp)|*.bmp|Gif Files (*.gif)|*.gif|Jpeg Files (*.jpg)|*.jpg|Png Files (*.png)|*.png|Tiff Files (*.tiff)|*.tiff||");
and the call is simple:
Code:
CFileDialogExt dlg(FALSE, NULL, NULL, OFN_FILEMUSTEXIST, szFilter); // <-- Saving operation
Ok, in order to catch file type changing, I override
CFileDialogExt::OnTypeChange(), just ike that:
Code:
void CFileDialogExt::OnTypeChange()
{
// TODO: Add your specialized code here and/or call the base class
CFileDialog::OnTypeChange();
CString sExt(GetFileExt());
switch(GetOFN().nFilterIndex)
{
case 1:
sExt.Format(_T("bmp"));
break;
case 2:
sExt.Format(_T("gif"));
break;
case 3:
sExt.Format(_T("jpg"));
break;
case 4:
sExt.Format(_T("png"));
break;
case 5:
sExt.Format(_T("tiff"));
break;
}
SetDefExt((LPCSTR)(LPCTSTR)sExt);
}
but the SetDefExt does work delayed with one step ... here is the behavior:
I type "test" as file name. I chose "
gif" extension. Nothing happen, the file name is the same.
I chose "
jpg". The file name is changing as "
test.gif".
I chose "
png". The file name is changing as "
test.jpg".
Do you know what I mean ? The file name is changing by the previous file extension choose.
I attach a test project to have a sample of what I wrote here ... Is there a solution to setup the right file name at the user file type choosing ?
Thank you.
TestFD.zip
↧
October 23, 2015, 11:26 am
I have written a program which concatenate two strings without the help of strcat.
I have used two loops to make this logic.
But want to make the logic with one loop kindly review my code
c++ program to concatenate two strings without using strcat and share your logic in comments section of blog so every visitor can read blog and your's code and compare both.
Thanks
↧
October 24, 2015, 11:55 am
Hello out there...
I will give you some background on my project before asking the question.
My current job now is automating test equipment. The device I'm working with now (a Lakeshore 335 Temperature Controller) had no provided source library in any language. So, I built my own Lakeshore class using the Win32API serial class (WriteFile, ReadFile) and standard C file I/O. This class was designed in a console environment.
Now, I'm porting my code to a gui environment. I need to be able to write status messages from the Lakeshore class to the form controls and be able to control a global instance of the Lakeshore class with the various forms. I've tried and failed twice with 2 different gui libraries.
Windows Forms Application: this class uses the System namespace (which is horrible and full of bugs). This class would not let me create form instances in other files.
Qt: this class did not like sharing instances either, but it was more flexible in terms of what you can do with it. I ended up having to merge the two classes into one set of files. It still doesn't load the program though.
One of the reasons I haven't used the Win32 API to make the gui is because I have a hard time visualizing a gui through code.
So, I am asking, is there a C++ or C gui library that has those capabilities?
This is not a beginner's question. I'm looking for an educated response.
Thanks.
Agent86
↧
October 25, 2015, 8:03 am
Never heard of this stuff before,
but is it possible or allowed?
Thanks
Jack
↧
↧
October 26, 2015, 5:07 am
I have an MS Office Addin, which is a toolbar developed using ATL COM. Now this addin is developed using different for different versions of MS Project or the source code is designed to run on multiple verisons of MS Office. Therefore in the stdafx.h, have a sample code...
Code:
#define OFFICE12 //2007
#define OFFICE14 //2010 // for office 2010
#define PROJECT12 //2007
#define PROJECT14 //2010 // for project 2010
#ifdef PROJECT12
#import "c:\\Program Files\\Microsoft Office\\OFFICE12\\msprj.olb" \
version("4.6") \
named_guids \
inject_statement("#import \"C:\\Program Files\\Common Files\\Microsoft Shared\\VBA\\VBA6\\VBE6EXT.OLB\" raw_interfaces_only named_guids no_function_mapping") \
inject_statement("using namespace VBIDE;") \
rename("Window", "MSPWindow") \
rename("Windows", "MSPWindows") \
no_function_mapping \
raw_interfaces_only \
raw_dispinterfaces
#endif
#ifdef PROJECT14
#import "c:\\Program Files (x86)\\Microsoft Office\\OFFICE14\\msprj.olb" \
version("4.6") \
named_guids \
inject_statement("#import \"C:\\Program Files\\Common Files\\Microsoft Shared\\VBA\\VBA6\\VBE6EXT.OLB\" raw_interfaces_only named_guids no_function_mapping") \
inject_statement("using namespace VBIDE;") \
rename("Window", "MSPWindow") \
rename("Windows", "MSPWindows") \
no_function_mapping \
raw_interfaces_only \
raw_dispinterfaces
#endif
Where the first para is for MS Office 2007 and and the 2nd paragraph is for MS Office 2014. Now comes the problem of MS Office 64-bit. If I am using 32-bit Windows and 32-bit MS Office, the path for the OLB file will be..
Code:
#import \"C:\\Program Files\\Common Files\\Microsoft Shared\\VBA\\VBA6\\VBE6EXT.OLB\
If I am using 64-bit Windows and 32-bit MS Office, the path of the OLB file will be..
Code:
#import \"C:\\Program Files (X86)\\Common Files\\Microsoft Shared\\VBA\\VBA6\\VBE6EXT.OLB\
And in case the Windows and MS Office are both 64-bit the path will be again
Code:
#import \"C:\\Program Files\\Common Files\\Microsoft Shared\\VBA\\VBA6\\VBE6EXT.OLB\
Now I want to provide a condition for these versions in stdafx.h. How can I accomplish this?
↧
October 26, 2015, 9:16 am
Hi,
I'm using the JMP instruction tecnique for try make a Anti-Dll Injection with detour when
LdrLoadDll api is called inside my program. I found a Delphi code that works perfectly, but this VC++ 2013 version for this code, crashes with a "Access Violation writing location 0x0000000000" with showed below.
So, how I can solve this trouble? Someone can help me please?
Thanks in advance.
Delphi version:
Code:
procedure hook(target, newfunc:pointer);
var
jmpto:dword;
OldProtect: Cardinal; // old protect in memory
begin
jmpto:=dword(newfunc)-dword(target)-5;
VirtualProtect(target, 5, PAGE_EXECUTE_READWRITE, @OldProtect);
pbyte(target)^:=$e9;
pdword(dword(target)+1)^:=jmpto;
end;
procedure myLdrLoadDll(PathToFile:PAnsiChar; Flags:variant; ModuleFileName:PAnsiChar; var ModuleHandle:THandle);
begin
MessageBox(0, 'I have blocked your attempt to inject a dll file!!', 'WARNING!', MB_OK);
ModuleHandle:=0;
end;
procedure Main;
begin
Hook(GetProcAddress(GetModuleHandle('ntdll.dll'), 'LdrLoadDll'), @myLdrLoadDll);
end;
begin
end.
Trying translate for VC++ 2013 version:
Code:
// Anti_DLLInjection.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <memory>
using namespace std;
BOOL TrampolineAPI(HMODULE hModule, LPCWSTR DllName, LPCSTR ProcName, DWORD dwReplaced)
{
DWORD dwReturn;
DWORD dwOldProtect;
DWORD dwAddressToHook = (DWORD)GetProcAddress(GetModuleHandle(DllName), ProcName);
BYTE *pbTargetCode = (BYTE *)dwAddressToHook;
BYTE *pbReplaced = (BYTE *)dwReplaced;
VirtualProtect((LPVOID)dwAddressToHook, 5, PAGE_EXECUTE_READWRITE, &dwOldProtect);
*pbTargetCode++ = 0xE9; // My trouble is here
*((signed int*)(pbTargetCode)) = pbReplaced - (pbTargetCode + 4);
VirtualProtect((LPVOID)dwAddressToHook, 5, PAGE_EXECUTE, &dwOldProtect);
dwReturn = dwAddressToHook + 5;
FlushInstructionCache(GetCurrentProcess(), NULL, NULL);
return TRUE;
}
void WINAPI Replaced(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType)
{
printf("Invasion!!");
}
int _tmain(int argc, _TCHAR* argv[])
{
while (true)
{
TrampolineAPI(GetModuleHandle(0), (LPCWSTR)"ntdll.DLL", "LdrLoadDLL", (DWORD)Replaced);
}
return 0;
}
↧
October 26, 2015, 2:20 pm
Hello to you all. Using Visual Studio 2012, I am currently testing a program of image segmentation and need to somehow pass a test image file in the main program as shown below. Been searching for a while. I would appreciate an example of howto use or replace the "argv" with the test image file and consequently produce an output file. Much appreciate your guidance!
http://cs.brown.edu/~pff/segment/[^]
Code:
int main(int argc, char **argv) {
if (argc != 6) {
fprintf(stderr, "usage: %s sigma k min input(ppm) output(ppm)\n", argv[0]);
return 1;
}
float sigma = atof(argv[1]);
float k = atof(argv[2]);
int min_size = atoi(argv[3]);
printf("loading input image.\n");
image<rgb> *input = loadPPM(argv[4]);
printf("processing\n");
int num_ccs;
image<rgb> *seg = segment_image(input, sigma, k, min_size, &num_ccs);
savePPM(seg, argv[5]);
printf("got %d components\n", num_ccs);
printf("done! uff...thats hard work.\n");
return 0;
}
↧