|
That is looking good, just a few things I would like to mention:
1. When you modify the value of a parameter, you are only modifying its value within the scope of the function, unless the parameter is passed by reference or you change the value of the pointed to value with a dereference like *p = NULL;
__declspec(dllexport)void FreeMem(void *Ptr) {
if(Ptr != NULL) {
free(Ptr);
}
}
2. sizeof(wchar_t *) * wcslen(String) + 1 is wrong for a few reasons when allocating space for the string.
The +1 needs to be multiplied by the size of each character as well sizeof(wchar_t *) * (wcslen(String) + 1)
Secondly, you need to multiply the number of characters by the size of each character, not the size of the pointer to each character sizeof(wchar_t) * (wcslen(String) + 1)
3. If you allocate a string as an array of wchar_t's rather than a pointer you can get the compiler to hard-code the size in, rather than having to calculate it at runtime.
This only works when the string is known at compile time, as it is in your case.
strlen is a somewhat slow function, it must iterate through every character in the string, check the value of the character and increment a counter. While this is ok if it only happens occasionally, you should prefer to use sizeof() when possible as it gets the compiler to do all this for you.
A similar principal applies for strcpy but to a lesser extent. memcpy can copy the data in chunks rather than 1 character at a time
__declspec(dllexport)void Stuff(wchar_t *&out) {
wchar_t String[] = L"test string whatever";
out = (wchar_t *)malloc(sizeof(String));
memcpy(out, String, sizeof(String));
}
Since you are iterating over this 10,000,000 times (at least in your example) you will notice an increase in performance
Finally, it would be better to return the pointer from Stuff()
__declspec(dllexport)wchar_t *Stuff() {
wchar_t String[] = L"test string whatever";
wchar_t *out = (wchar_t *)malloc(sizeof(String));
memcpy(out, String, sizeof(String));
return out;
}
[DllImport("testdll.dll")]
public static extern IntPtr Stuff();
|
|
|
|
|
Great answer! Thanks very much
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
There are two possible solutions:
1. If the data structure in question is trivial and can be trivially allocated and deallocated, delegate the allocation to the caller. this is what many standard functions such as strcpy() do.
2. If the data structure requires some attention upon allocation, or contains arrays of dynamical (and potentially at the time of creation unknown) length, then the above suggested method is preferable, i. e. you allocate the structure and provide a function to deallocate it appropriately at a later time.
Personally I prefere the first method whereever possible, because it is clearer from the point of view of the caller: it is much easier to spot a missing delete or free that should have been paired with a new or malloc, than spotting a missing call to a deallocation function which should be paired with some "get" or "read" function. It may not be implicitely clear that a function that provides some data also allocates the structure (it might just as well provide a pointer to a structure that is allocated and accordingly freed elsewhere!)
|
|
|
|
|
Title says it all. After all my searching I have not been able to find a decent tutorial or sample code that will teach me how to implement and customize a TabControl function in a Visual C++, Win32, Win32 Project environment. A link or download link to this would greatly be appreciated. Please and thank you.
|
|
|
|
|
You could have put everything into the title itself.
|
|
|
|
|
I think you didn't google it properly. Few of the links I found:
http://www.codeproject.com/KB/tabs/SimpleTab.aspx
http://www.codeguru.com/cpp/controls/controls/tabcontrols/article.php/c7407
http://www.codeproject.com/KB/tabs/AMCustomTabCtrlDemo.aspx
http://www.codeproject.com/KB/tabs/ctabctrlssl.aspx
I haven't gone through the details of the above articles. But after having over look, I can say they might help you to start over (as your problem is not specific. What I've perceived is you just wanna to customize the tab view control).
|
|
|
|
|
I think he was after Win32 API style, not MFC.
|
|
|
|
|
|
If you are wanting to do it with Win32 API and not MFC, I would still suggest checking out the MFC samples that Malli_S linked you to, because it is pretty much the same principal.
See Subclassing a window on MSDN[^]
What you need to do is write your own message procedure for the tab control
WNDPROC g_pOldProc;
LRESULT APIENTRY MyTabProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam) {
switch (nMsg) {
case WM_SIZE:
break;
}
return CallWindowProc(g_pOldProc, hWnd, nMsg, wParam, lParam);
}
g_pOldProc = (WNDPROC)SetWindowLong(hTabCtrl, GWL_WNDPROC, (LONG)&MyTabProc);
g_pOldProc = (WNDPROC)SetWindowLongPtr(hTabCtrl, GWL_WNDPROC, (LONG_PTR)&MyTabProc);
You need to restore the old message procedure upon the parents destroy (refer to the sample linked above)
|
|
|
|
|
Hello,
I want to parse the song and video files ....so please tell me anyone how i will do it in MFC.
Ajit K
|
|
|
|
|
Parse a song or video ?
What do you mean by that ? Could you please describe what you are trying to do a bit more clearly ?
|
|
|
|
|
The question sound like:
"I want to elaborate my car engine ... so please tell me anyone how I will do it with my screwdriver."
Do you get the point?
2 bugs found.
> recompile ...
65534 bugs found.
|
|
|
|
|
|
Hello,
Actually i want to get the id of song ,name of song, path of song , duration ,clipinfo if its a video etc using MFC/C++.
Thanks
Ajit
|
|
|
|
|
mozilla firefox will store all bookmarks in places.sqlite file... i am writing small utility which will sync two places.sqlite file bookmarks....
|
|
|
|
|
If I understand your question rgith, there is no need to have any knowledge of the internals in this case.
The easiest thing to do would be to compare the file date and replace the oldest one with the newer.
modified 13-Sep-18 21:01pm.
|
|
|
|
|
no... original file also modified or added with some bookmarks and copied file also modified with some other bookmarks in that we cannot just replace ...
|
|
|
|
|
I'm guessing it's an SQLite database file. Standard procedure is to use SQL. Connect to the database and execute your queries. There are also C APIs[^] for doing this, still needing SQL though. It's well documented and there are plenty of samples around. Use sqlite3_open_v2()[^] to open the file, and sqlite3_exec()[^] to execute your queries.
Good luck!
|
|
|
|
|
I have to add a comment like //<amrit.agrawal> Added 02/02/2011 in somewhere in the code.
When we made some some changes in any file and want to add a tag so that it can be track afterwords to know which developer is made changes on that particular file or line, in a shortcut key like Alt + a.
|
|
|
|
|
Read the file using CStdioFile(or some other class or file object) and insert the comment at the location "somewhere in the code". Also add the developer name..
BTW your requirement seems to be just like reading and writing to a file or is there something else that you intend to do? Clarify please.
I am a HUMAN. I have that keyword in my name........
_AnsHUMAN_
|
|
|
|
|
Suppose I am working to solve a bug in some function. I want to add a chunk of code in that function. Above that chunk I have to write like( While Press a shortcut key like Alt + s ), //<developer_name> Added <current date=""> so that I can identify that this changes has done by me. Otherwise I have to do that manually every time.
|
|
|
|
|
I am really under the impression that you need a version control system (VCS), like I suggested below. Comments in code just won't do it, no matter how clever your tricks to get them inserted. The huge advantage of a VCS is that you will not only see which line had been changed by whom (and the related comment), but also what else had been changed at the same time, and even what, exactly, the changes were. You can also undo the changes, or restore any previous version that you want, even deleted files can be restored this way.
In case of Subversion I could give you some pointers. E. g. on Windows, you can just install the GUI client TortoiseSVN instead. it comes with terrific documentation that will get you started quite easily. (not saying the Subversion manual is bad, it's just targeted a bit more at advanced users). There are also plugins for integration with VisualStudio, and Eclipse supports it out of the box.
If you have Visual Studio 2010, TFS would be an option (the version of TFS delivered with earlier versions of VS weren't all that good). AFAIK it's free if you have the subscription.
There are other systems such as Mercurial and GIT, but I know too little of these to give any further advice.
|
|
|
|
|
Adding comments won't do it, because it may be gone if someone later deletes a function that has become obsolete, all the comments therein are gone as well. Of course, if that function became obsolete, you may no longer be interested in the changes going into it.
Also, the (in)famous habit of copy and paste may obfuscate or invalidate some of these comments, when replicating code to different contexts.
The best way to track changes is using a version control system, such as subversion. These systems not only allow you to add comments on your changes outside your source code, they also let you see all files that have been changed at the same time for a particular fix. It's also much easier to use: change 15 files to fix a problem, then check in and add a comment about what you've done just once. Much easier than adding comments on 50 lines in 15 different files!
I've suggested subversion because that is what I am familiar with, and I know it works on many platforms and there are lots of addons and plugins to integrate it with your development environment. But there are many other good version control systems as well.
|
|
|
|
|
How to set Background color
Header File "MyButton.h"
#pragma once
#include "afxwin.h"
const COLORREF bkColor= RGB(0,0,0);
class CMyButton :
public CButton
{
public:
CMyButton(void);
public:
~CMyButton(void);
protected:
afx_msg void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
DECLARE_MESSAGE_MAP()
};
Cpp File "MyButton.cpp"
#include "StdAfx.h"
#include "MyButton.h"
CMyButton::CMyButton(void)
{
}
CMyButton::~CMyButton(void)
{
}
BEGIN_MESSAGE_MAP(CMyButton, CButton)
ON_WM_DRAWITEM()
ON_WM_ERASEBKGND()
END_MESSAGE_MAP()
BOOL CMyButton :: OnEraseBkgnd(CDC* pDC)
{
CRect rect;
GetClientRect(rect);
pDC->FillSolidRect(rect,bkColor);
return true;
}
void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) {
}
Now u can use above class in your Dialog As:-
BOOL CChessGameDlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE);
SetIcon(m_hIcon, FALSE);
CMyButton* m_pbtn13 = new CMyButton ();
m_pbtn13 ->Create(NULL, WS_CHILD | WS_VISIBLE |BS_OWNERDRAW ,CRect(150, 250, 250, 300), this, NZWR_BUTTON_13);
return TRUE;
}
HOW to set an Icon to CButton
BOOL CChessGameDlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE);
SetIcon(m_hIcon, FALSE);
CButton m_pbtn12 = new CButton ();
m_pbtn12 ->Create(NULL, WS_CHILD | WS_VISIBLE |BS_ICON ,CRect(150, 50, 250, 200), this, NZWR_BUTTON_12);
if (m_pbtn12->GetIcon() == NULL)
m_pbtn12->SetIcon(::LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE( IDI_ICON1)));
return TRUE;
}
How to use CBitmapButton Class
BOOL CChessGameDlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE);
SetIcon(m_hIcon, FALSE);
m_pbtn11= new CBitmapButton ();
m_pbtn11->Create(NULL, WS_CHILD | WS_VISIBLE | BS_OWNERDRAW,CRect(50, 50, 200, 200), this, NZWR_BUTTON_11);
m_pbtn11->LoadBitmaps(IDB_BITMAP2, IDB_BITMAP1, IDB_BITMAP2, IDB_BITMAP1);
m_pbtn11->SizeToContent();
return TRUE;
}
/pre;
|
|
|
|
|
Were you intended to post 'Tip/Tricks' to CP ?
|
|
|
|
|