Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Inserting into msi database using C++

26 views
Skip to first unread message

Amar

unread,
Sep 23, 2009, 6:26:02 PM9/23/09
to
HI All,

I want to insert a row in CA table rather than using

CScript and WinRunSQl.vbs i want to write my own C++ code to do that.

I did in the following way

void ExecuteQueryForInsertIntoMSIDB()
{

PMSIHANDLE hDatabaseForInsert = 0;
PMSIHANDLE hViewForInsert = 0;
PMSIHANDLE hRecForInsert = 0;

if (ERROR_SUCCESS == MsiOpenDatabase(_T("C:\\Install.msi"),
MSIDBOPEN_TRANSACT, &hDatabaseForInsert))
{

if (ERROR_SUCCESS == MsiDatabaseOpenView(hDatabaseForInsert, _T("INSERT
INTO 'CustomAction'('Action','Type','Source','Target') VALUES ('', 1
,'','InstallCopy')"), &hViewForInsert))
{

hRecForInsert = MsiCreateRecord(1);
if (ERROR_SUCCESS == MsiViewExecute(hViewForInsert, hRecForInsert)
&& ERROR_SUCCESS == MsiViewClose(hViewForInsert)
&& ERROR_SUCCESS == MsiDatabaseCommit(hDatabaseForInsert))
{
//
// New data successfully committed to the database
//
}

}
}
}

But i am not able to see any row in the CA table. Can some suggest me what i
am doing wrong here?

Amar

unread,
Sep 23, 2009, 6:36:02 PM9/23/09
to

I also forgot to delete the line mentioned below. That is

hRecForInsert = MsiCreateRecord(1);

I just added, i tried without this line of code also it gives me the same
error

Regards,
Amar.

Kalle Olavi Niemitalo

unread,
Sep 24, 2009, 12:07:47 AM9/24/09
to
Amar <Am...@discussions.microsoft.com> writes:

> if (ERROR_SUCCESS == MsiDatabaseOpenView(hDatabaseForInsert, _T("INSERT
> INTO 'CustomAction'('Action','Type','Source','Target') VALUES ('', 1
> ,'','InstallCopy')"), &hViewForInsert))

That should be `CustomAction` rather than 'CustomAction'.
Likewise in the column names.
And don't leave CustomAction.Action empty.

Errors like this should cause MsiDatabaseOpenView or
MsiViewExecute to return something other than ERROR_SUCCESS,
I think. Your source code does check for errors
but I don't see anything there to report the errors it finds.
Thus, I suspect one of the calls returned an error
but you didn't know about it. Did you step through the code
with a debugger, to see how far it gets?

Amar

unread,
Sep 24, 2009, 10:02:02 PM9/24/09
to
Hi Kalle Olavi Niemitalo,

Thank you for your response. It worked fine while debugging.
But when i do the same in release mode it is not working. I am doing this as
a post build event. Why that is not working in release mode i am confused and
trying, no idea, can you please suggest me?

Amar.

Wilson, Phil

unread,
Sep 25, 2009, 12:30:19 PM9/25/09
to
You'll have to define "doesn't work" a bit better. Since the code you posted
has absolutely no error reporting you should add some. It might be as
trivial as being unable to find the MSI file now you're using Release
folders.
--
Phil Wilson
The Definitive Guide to Windows Installer
http://www.apress.com/book/view/1590592972


"Amar" <Am...@discussions.microsoft.com> wrote in message
news:239C7F0A-AC9C-45A6...@microsoft.com...

Amar

unread,
Sep 25, 2009, 4:08:03 PM9/25/09
to
Hi Phil,

I added the error code also to debug and test.

What exactly is happening here is. If i do F5 and debug the code step by
step it works without any problem. But when i do build in debug mode without
debugging it is not updating the msi with the values what i want to enter. i
am executing the below mentioned code as a post build event.

Why is that so?

Amar

Amar

unread,
Sep 25, 2009, 5:25:02 PM9/25/09
to
Hi Phil and Kalle Olavi Niemitalo,

Thanks for your suggestions. It worked find now in both release and debug
modes, i did a small change in settings which created all these problems.

Thanks again.

Amar

0 new messages