473,785 Members | 2,309 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can't find a way to get the ICeeGen interface

Hi,

1. I am writing some kind of a CLI Linker
2. I am using the unmanaged meta-data API.
I wonder browsing "cor.h" I have encountered the 'ICeeGen' this interface is retrieved by the 'ICorModule' interface, I didn't find any API that provide me the means to get an 'ICorModule' interface, how should I get this interface? What object should i create? what method should I call?

Any comments remarks or pointers will be appreciated.

--
Nadav
http://www.ddevel.com
Nov 17 '05 #1
7 1941
> 1. I am writing some kind of a CLI Linker
2. I am using the unmanaged meta-data API.
I wonder browsing "cor.h" I have encountered the 'ICeeGen' this interface is retrieved by the 'ICorModule' interface, I didn't find any API that provide me the means to get an 'ICorModule' interface, how should I get this interface? What object should i create? what method should I call?

Any comments remarks or pointers will be appreciated.

Hello Nadav

short answer is: call
EXTERN_C HRESULT __stdcall CreateICeeFileG en(ICeeFileGen **ceeFileGen);
exported from mscorpe.dll.

Have a look at ICeeFileGen.h.

I'm currently in the process to make this work myself and will post a
longer answer tomorrow if possible.

hth
--
Ben
http://bschwehn.de
Nov 17 '05 #2
Hi Ben,

Thanks for your response, 'CreateICeeFile Gen' is used to create an
ICeeFileGen Object and NOT ICeeGen Object, ICeeFileGen is used to emit an
assembly to the disk, however usage of this interface require access to data
not exposed by IMetaDataEmit, this require the usage of an additional objects
PELoader, ICeeGen, ... ( objects OTHER THEN ICeeFileGen ), from some
additional research I have done, I have found the sscli ( search through the
inet ) library very useful, it has the source code of the CreateICeeFileG en (
through the CCeeFileGenWrit er class ) and have the source code for CCeeGen (
which implements the ICeeGen interface ), all of this and much more is open
source and free of charge...

P.S.
We could discuss these Issues in details through Windows Messenger, you can
find me with the following e-mail address: nadavr AT yahoo DOT com

Nadav.

"Ben Schwehn" wrote:
1. I am writing some kind of a CLI Linker
2. I am using the unmanaged meta-data API.
I wonder browsing "cor.h" I have encountered the 'ICeeGen' this interface is retrieved by the 'ICorModule' interface, I didn't find any API that provide me the means to get an 'ICorModule' interface, how should I get this interface? What object should i create? what method should I call?

Any comments remarks or pointers will be appreciated.

Hello Nadav

short answer is: call
EXTERN_C HRESULT __stdcall CreateICeeFileG en(ICeeFileGen **ceeFileGen);
exported from mscorpe.dll.

Have a look at ICeeFileGen.h.

I'm currently in the process to make this work myself and will post a
longer answer tomorrow if possible.

hth
--
Ben
http://bschwehn.de

Nov 17 '05 #3
> Thanks for your response, 'CreateICeeFile Gen' is used to create an
ICeeFileGen Object and NOT ICeeGen Object, ICeeFileGen is used to emit an
oops, sorry, didn't read your posting carefully enough.
assembly to the disk, however usage of this interface require access to data
not exposed by IMetaDataEmit, this require the usage of an additional objects
PELoader, ICeeGen, ... ( objects OTHER THEN ICeeFileGen ),
hmm, why?

Perhaps you're trying to accomplish something different than I do, but I
got the following to work by just using the ICeeFileGen object:

- Open a assembly with IMetaDataEmit, IMetaDataImport etc.
- Create a new Assembly (.exe) with ICeeFileGen, copy the metadata in
it, copy all the IL code of all methods
- save the new file.

this gets me a working executable. Well pretty much at least, for now
I'm still having some problems with exception handling code not being
copied properly and I'm not sure what to do with embeded ressources, so
the PE file does not verify with peverify yet, but simple hello world
stuff is already working

It should he easy changing the IL code, adding new Methods etc. So this
is good enough for me :) and also seems what you're trying to do as
described in your earlier post "Assembly emmition problem"...?
from some
additional research I have done, I have found the sscli ( search through the
inet ) library very useful, it has the source code of the CreateICeeFileG en (
through the CCeeFileGenWrit er class ) and have the source code for CCeeGen (
which implements the ICeeGen interface ), all of this and much more is open
source and free of charge...
I was considering going this path as well, one problem I find with this
(other than the sheer inconvenience) is that it's not going to be
updated for new Versions of the frameweok, so I'm not sure if you won't
get into problems when dealing with 2.0 assemblies containing generics
and so on.
Using mscorpe.dll provides you with an up-to-date class. Also the 2.0
version of the ICeeFileGen class has a few new methods that seem worthy
of further exploration like CreateCeeFileEx 2 (takes an existing assembly
as a "seed file")
We could discuss these Issues in details through Windows Messenger, you can
find me with the following e-mail address: nadavr AT yahoo DOT com


I'm not currently using IM so I'd prefer sticking to email and usenet...
Have a you been able to find any documentation on this stuff?
My main help was Benny/29A's article
"MSIL-PE-EXE infection strategies" on
http://vx.netlux.org/lib/vbe00.html and the source code to
"I-Worm.Serotonin" (in assembler :/ )

--
Ben
http://bschwehn.de
Nov 17 '05 #4
> Hi, thanks for your immediate responce, I wonder... how did you get to copy
the IL Code???, the IMetaDataEmit interface deals only with the metadata, as
I understand I have to manually find the ".text" segment of the PE, this
segment include the IL code, then I use the RVA retrieved from the method
properties to extract the IL code BUT the new generated EXE doesn't work, I
guess I am doing something wrong here.... how did you get to copy the IL
Code??? any samples would be appriciated...


I'm at work atm so I don't have access to my code but i will post a code
sample tonight.

Until then:
Basically what I do is is call EnumMethods for all types, call
GetMethodProps, this gets me the codeRva which can be translated to get
a pointer to the IL code of the memory mapped file (method RvaToVa32 or
similar in imagehlp.lib), then I get the size of the IL code plus any
MORE_SECTS (containing the exception handling and other stuff).

Then I copy all of this into a buffer (all IL code for all methods,
you'll have to do some DWORD-aligning as well) and call
ICeeGen->GetILSection , then GetSectionBlock to allocate a buffer in the
ilsection, memcpy my IL buffer in there.
I also call SetRVA for every method because the RVA can change.

Well, it should become clearer when I post some sample code tonight.

i tested the assembly with peverify and this reports no errors (i found
the error wrt excption code in my last post).

I haven't tested it with embedded resources yet, this probably needs
more work...

have a look at http://vx.netlux.org/lib/vbe00.html

--
Ben
http://bschwehn.de
Nov 17 '05 #5
Hi Ben, Thanks for your responce, I wonder..., Did you used GetSectionBlock
to get a single block for the IL of all of the methods or you call
GetSectionBlock for each new method? do you call GetSectionRVA for each
method or do you call it once and calculate the RVA incrementally?
I am using 'COR_ILMETHOD:: Emit' to emit the method header/signature and
memcpy to copy the IL Code, Still, I am doing something wrong... using ildasm
I can see that each of the 'copied' methods has a codesize of 0...
The following is a simpified snap of the code i use:
while(SUCCEEDED (hr = m_spMetaDataImp ort->EnumMethods(&e nr
, tkClass
, &tok
, 1
, &count)) && count > 0)
{
hr = m_spMetaDataImp ort->GetMethodProps (tok
, &tkClass
, wszType
, sizeof(wszType)/sizeof(*wszType )
, 0
, &dwMethodAtt r
, &pMethodSignatu re
, 0
, &dwCodeRVA
, &dwImplFlags );
Loader.getVAfor RVA(dwCodeRVA, (void**)&pILHea der)
COR_ILMETHOD_DE CODER method(pILHeade r);
unsigned headerSize = COR_ILMETHOD::S ize(&pILHeader->Fat, TRUE);

unsigned codeSizeAligned = pILHeader->Fat.GetCodeSiz e();
unsigned totalSize = headerSize + codeSizeAligned ;

unsigned align = 4;
if (headerSize == 1)// Tiny headers don't need any alignement

align = 1;

hr = pContext->FileGen->GetSectionBloc k(
pContext->IlSection
, totalSize
, align
, (void**)&raw_il _section);
raw_il_section += COR_ILMETHOD::E mit(headerSize
, &pILHeader->Fat
, TRUE
, raw_il_section) ;
memset(raw_il_s ection, 0, codeSizeAligned );

memcpy(raw_il_s ection, pILHeader->Fat.GetCode( ), codeSizeAligned );

raw_il_section += codeSizeAligned ;
hr = m_spMetaDataEmi t->SetRVA(tok, pContext->IlSectionRVA );
pContext->IlSectionRVA += totalSize;
hr = pContext->FileGen->LinkCeeFile(pC ontext->ceeFile);
}
"Ben Schwehn" wrote:
Hi, thanks for your immediate responce, I wonder... how did you get to copy
the IL Code???, the IMetaDataEmit interface deals only with the metadata, as
I understand I have to manually find the ".text" segment of the PE, this
segment include the IL code, then I use the RVA retrieved from the method
properties to extract the IL code BUT the new generated EXE doesn't work, I
guess I am doing something wrong here.... how did you get to copy the IL
Code??? any samples would be appriciated...


I'm at work atm so I don't have access to my code but i will post a code
sample tonight.

Until then:
Basically what I do is is call EnumMethods for all types, call
GetMethodProps, this gets me the codeRva which can be translated to get
a pointer to the IL code of the memory mapped file (method RvaToVa32 or
similar in imagehlp.lib), then I get the size of the IL code plus any
MORE_SECTS (containing the exception handling and other stuff).

Then I copy all of this into a buffer (all IL code for all methods,
you'll have to do some DWORD-aligning as well) and call
ICeeGen->GetILSection , then GetSectionBlock to allocate a buffer in the
ilsection, memcpy my IL buffer in there.
I also call SetRVA for every method because the RVA can change.

Well, it should become clearer when I post some sample code tonight.

i tested the assembly with peverify and this reports no errors (i found
the error wrt excption code in my last post).

I haven't tested it with embedded resources yet, this probably needs
more work...

have a look at http://vx.netlux.org/lib/vbe00.html

--
Ben
http://bschwehn.de

Nov 17 '05 #6
Hello Nadav

This is what I do:

[error checking etc removed]
_pCeeFileGen->CreateCeeFile( &_hCeeFile);
_pCeeFileGen->SetOutputFileN ame(_hCeeFile, fileName);

//call link so we can use GetSectionRVA
_pCeeFileGen->LinkCeeFile(_h CeeFile);

//get il section
HCEESECTION hSection;
_pCeeFileGen->GetIlSection (_hCeeFile, &hSection);

ULONG rva;
_pCeeFileGen->GetSectionRV A (hSection, &rva);

ULONG codeSize =0;
char* pBytes = NULL;

//get a new block just to get a pointer to the section, get 4 bytes for
DWORD alignement

_pCeeFileGen->GetSectionBloc k (hSection, 4, 1,
reinterpret_cas t<void**>(&pByt es));

unsigned int offset;
_pCeeFileGen->ComputeSection Offset(hSection , pBytes, &offset);

//this copies all IL code into a buffer (_pILBuffer), see below
//also calls SetRVA for each method
GetILCode(&code Size, rva + offset);

//get a new block large enough to hold IL Code (codezize - 4 bytes
//allocatd above)

char* pBytes2 = NULL;
_pCeeFileGen->GetSectionBloc k (hSection, codeSize - 4, 1,
reinterpret_cas t<void**>(&pByt es2));

//copy IL Code to section
memcpy(pBytes, _pILBuffer, codeSize);

_pCeeFileGen->EmitMetaDataEx (_hCeeFile, _pMetaDataEmit) ;
_pCeeFileGen->LinkCeeFile(_h CeeFile);
_pCeeFileGen->GenerateCeeFil e(_hCeeFile);
_pCeeFileGen->DestroyCeeFile (&_hCeeFile);
delete[] _pILBuffer;

//==== GetILCode
HRESULT CAssemblyWriter ::GetILCode(ULO NG* pCodeSize, unsigned int offset) {
HRESULT hr = NULL;
//HACK: Allocate enough memory to hold entire .text section
int bufferSize = _pReader->GetPEFile()->_sizeOfTextSec tion;
_pILBuffer = new BYTE[bufferSize];
ZeroMemory(_pIL Buffer, bufferSize);

int codeSize = 0;
//get all method of all types and add ILCode to buffer
std::vector<CTy peDef*> typeDefs = _pReader->_typeDefs;
for (unsigned int ii=0; ii < _pReader->_typeDefs.size (); ii++) {
CTypeDef* pType = _pReader->_typeDefs[ii];
for (unsigned jj = 0; jj < pType->_methodDefs.si ze(); jj++) {
CMethodDef* pMethod = pType->_methodDefs[jj];
if (pMethod->_codeSize > 0) {
int methodSize = pMethod->_codeSize + pMethod->_codeHeaderSiz e;
memcpy(_pILBuff er + codeSize, pMethod->_pCodeHeader , methodSize);

hr = _pMetaDataEmit->SetRVA(pMeth od->_token, offset + codeSize);
if (!SUCCEEDED(hr) ) {
return hr;
}
//HACK
if (pMethod->_name == L"Main") {
hr = _pCeeFileGen->SetEntryPoint( _hCeeFile, pMethod->_token);
if (!SUCCEEDED(hr) ) {
return hr;
}
}
codeSize += methodSize;
codeSize = _ALIGN4(codeSiz e);
if (pMethod->_pStartExtraSe ctions) {
memcpy(_pILBuff er + codeSize, pMethod->_pStartExtraSe ctions,
pMethod->_sizeExtraSect ions);
codeSize += pMethod->_sizeExtraSect ions;
codeSize = _ALIGN4(codeSiz e);
}
}
}
}
*pCodeSize = codeSize;
return hr;
}
as you can see, I have all TypeDefs and methods in std::vectors

pMethod->_codeSize is the size of the IL Code and pMethod->_pCodeHeader
points to the start of the ILCode Header (CorILMethod_Sm allFormat etc)

pMethod->_pStartExtraSe ctions points to the extrasections like exception
handling if present
(http://msdn.microsoft.com/msdnmag/is...I/default.aspx)

to translate the rva obtained from GetMethodProps I call

ImageRvaToVa(pN THeaders, pBase, rva, NULL);

hth

--
Ben
http://bschwehn.de
Nov 17 '05 #7
> Thanks fore your responce it was very helpful, I have managed to get the
Assembly copied, I have also managed to copy Exceptions functionality ( e.g.
COR_ILMETHOD_SE CT_EH ) and to extract and update the entrypoint information,
I can send you a working sample...


I'm glad it helped a bit...
A sample would be nice (my email address is valid) but really only if
it isn't too much trouble. I got most things working myself now, however
I haven't yet looked into copying unmanaged embedded resources and
native code in a mixed mode module...
--
Ben
http://bschwehn.de
Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
19338
by: Prabu | last post by:
Hi, I'm new to python, so excuse me if i'm asking something dumb. Does python provide a mechanism to implement virtual functions? Can you please give a code snippet also...:) Thanx in advance -Prabu.
6
2036
by: Paul | last post by:
Hello everyone: I am developing a VB.Net Windows Application and I am now ready to create the deployment project for it. This application needs to be installable on a different number of users / clients, and the application has an Access DB out on their network. How can I create a deployment routine where I can have a dialog box prompt for the network share (i.e. \\server\dbfolder) at install-time? Once network the share is specified...
39
6554
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. When it completes, it can call a success, or a failure function. The names of these success, or failure functions will differ, and I'd like to know how I can pass the name of a function to my tool, and how my tool can call the function, using that...
4
3617
by: chellappa | last post by:
hi all, i trying to find the intferface name using this coding ,but that is not working ,,,if anybody this pervious..please help to find the interface name using Linux C. //CODE ioctl(sd, SIOCGIFNAME, &ifr); strncpy(ifname,ifr.ifr_name,IFNAMSIZ); printf("Interface name :%s\n",ifname);
22
23385
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete examples?
15
12792
by: jon | last post by:
How can I call a base interface method? class ThirdPartyClass :IDisposable { //I can not modify this class void IDisposable.Dispose() { Console.WriteLine( "ThirdPartyClass Dispose" ); } } class MyClass :ThirdPartyClass, IDisposable { void IDisposable.Dispose() {
1
2055
by: skootr | last post by:
I have a Public Interface defined in a class module. I also have a form that implements that interface After building the solution, I added an Inherited Form (inherited from the above-mentioned form) to the project via the IDE However, when I drop down the "(base class events)" list, I can't find the methods defined in the interface. I know the interface has been inherited because if I enter an "Implements" statement in the derived...
8
1785
by: John | last post by:
What is the purpose / benefit of using an interface statement? It doesn't seem like anything more than a different way to make a class... (except you can't define any procedures in an interface like you can do in a class). I'm obviously missing something... John
5
2311
by: SunnyDrake | last post by:
HI! I wrting some program part of it is XML config parser which contains some commands(for flexibility of engenie). how do i more simple(if it possible not via System.Reflection or System.CodeDom.CodeCastExpression) __problem typecast #1 Desc:i do needed checks but data/commands in XML is dynamic and i don't wanna fix C# code again and again... Sample:foreach (object some in somearray) (some.GetType())some.someaction();
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10356
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5396
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4061
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2893
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.